Presentation is loading. Please wait.

Presentation is loading. Please wait.

Events and Event Handling

Similar presentations


Presentation on theme: "Events and Event Handling"— Presentation transcript:

1 Events and Event Handling
CS140 Dick Steflik

2 series of events GUI is displayed and system is running it's event loop user does something (clicks mouse button) system recognizes whatever it was and creates an appropriate event object (MouseEvent) and passes it to the handler that has been registered for that event (mouseClicked()) the handler executes the handling code system returns to event loop handlers are registered with the system when a listener is added to a component

3 java.awt.event provides interfaces and classes for handling events generated by AWT components

4 ActionEvent An event which indicates that a component-defined action occurred. This high-level event is generated by a component (such as a Button) when the component-specific action occurs (such as being pressed). The event is passed to every ActionListener object that registered to receive such events using the component's addActionListener method.

5 Swing Listeners action listeners – listens for user actions on registered components component listener – listens for changes in the component's size, position or visibility focus listener – listens for whether the component gained or lost the keyboard focus key listener – listens for key presses, key events are fired by the component currently having keyboard focus mouse listener – listens for mouse clicks, mouse presses, mouse releases or mouse movement into or out of the components drawing area mouse-motion listener – listens for changes in the mouse cursor's position over the component mouse-wheel listener - listens for mouse wheel movement over the component Hierarchy listener – listens for changes to a component's containment hierarchy of changed events Hierarchy bounds listener – listens for changes in a component's containment hierarchy of moved and resized events

6 Listener Interfaces Listeners are defined as java interfaces with a listener interface for each event type ex the ActionListener interface has only one method: actionPerformed(ActionEvent e) the MouseListener has five methods: mouseClicked(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e) all method must be defined (even if with an empty body) unless tou use an Adapter class

7 Adapters Adapter is a pattern that provides default (often empty) implementation of interface methods or abstract classes. For example MouseAdapter provides empty implementation of MouseListener interface methods. You only need to override the methods you need to implement. It is useful because very often you do not really use all methods declared by interface, so implementing the interface directly is very wordy.

8 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // do something } });


Download ppt "Events and Event Handling"

Similar presentations


Ads by Google