8
EVENTS AND EVENT HANDLING Object Oriented Programming

10 Events and Event Handling

Embed Size (px)

DESCRIPTION

OOP

Citation preview

  • EVENTS AND EVENT HANDLING Object Oriented Programming

    Advanced Object-Oriented Concepts (Part 1)

  • Events and Event HandlingLike all Java classes, events are Objects that the user initiates.The parent class for all event objects is named EventObject, which descends from the Object class. EventObject is the parent of AWTEvent, which in turn is the parent of specific event classes such as ActionEvent and ComponentEvent.

    Events and Event Handling*

    Advanced Object-Oriented Concepts (Part 1)

  • Events and Event HandlingCommon user actions and the events that are generated from them

    *When you write programs with GUI interfaces, you are always handling events that originate with the mouse or keys on specific Components or Containers.

    Events and Event Handling*

    User ActionResulting Event TypeClick a buttonActionEventClick a componentMouseEventClick an item in a list boxItemEventClick an item in a check boxItemEventChange text in a text fieldTextEventOpen a windowWindowEventIconify a windowWindowEventPress a keyKeyEvent

    Advanced Object-Oriented Concepts (Part 1)

  • Event ListenersEach event class shown in the table has a listener interface associated with it so that for every event class, such as Event, there is similarly named Listener interface. Every Listener interface method has the return type void, and each takes one argument an object that is an interface of the corresponding Event class. Events and Event Handling*

    Advanced Object-Oriented Concepts (Part 1)

  • Events ListenerEvent listeners and the types of events for which they are used

    Events and Event Handling*

    ListenerType of EventsExampleActionListenerAction EventsButton ClicksAdjustmentListenerAdjustment EventsScrollbar eventsChangeListenerChange EventsSlider is repositionedFocusListenerKeyboard Focus EventsTexfield gains or loses focusItemListenerItem EventsChange box change statusKeyListenerKeyboard EventsText is enteredMouseListenerMouse EventsMouse clicksMouseMotionListenerMouse Movement EventsMouser rollsWindowListenerWindow EventsWindow closes

    Advanced Object-Oriented Concepts (Part 1)

  • Event ListenersAny object can be notified of an event as long as it implements the appropriate interface and is registered as an event listener of the appropriate event source. The class of the object that responds to an event must contain a method that accepts the event object created by the users action. In other words, when you register a component (such as JFrame) to be a listener for events generated by another component (such as a JCheckBox), you need to write a method that reacts to any generated event. Events and Event Handling*

    Advanced Object-Oriented Concepts (Part 1)

  • Using AWTEvent Class MethodsUseful Event Class Method

    Events and Event Handling*

    ClassPurposeEvenObjectReturns the Object involved in the eventComponentEventReturns the Component involved in the eventWindowEventReturns the Window involved in the eventItemEventReturns an integer named getStateChange()ItemEvent.SELECTED or ItemEvent.DESELECTED

    Advanced Object-Oriented Concepts (Part 1)

  • Handling Mouse EventsThe MouseMotionListener interface provides methods named mouseDragged() and mouseMoved() that detect the mouse being rolled or dragged across a component surface. The MouseListener interface provides methods named mousePressed(), mouseClicked(), and mouseReleased() that are similar to the keyboard event methods keyPressed(), keyTyped(), and keyReleased(). The MouseInputListener interface implements all the methods in both the MouseListener and MouseMotionListener interfacesEvents and Event Handling*

    Advanced Object-Oriented Concepts (Part 1)

    *