Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.

Similar presentations


Presentation on theme: "Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event."— Presentation transcript:

1 Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event. n Event Listeners: A listener is an object that is notified when an event occurs. ä It must have been registered with one or more sources to receive notification about specific types of events, ä It must implement methods to receive and process these notification.

2 The Event delegation Model n Event classes n Event listeners n Explicit event enabling n Adapters

3 The Event Classes n GUIs are event-driven, which generate events when the user of the program interacts with the GUI. n When a user interaction occurs an event is automatically sent to the program. n GUI information is stored in an object of a class that extends AWTEvent.

4 Event Class Hierarchy java.util.EventObject java.awt.AwtEvent ActionEventAdjustmentEvent ComponentEventItemEvent TextEvent ContainerEventFocusEventInputEventPaintEventWindowEvent KeyEventMouseEvent

5 The Event Classes n java.util.EventObject ä Object getSource(): returns the object that originated the event. n java.awt.AWTEvent ä int getID(): returns the ID of the event

6 The Event Classes n ActionEvent: generated by activation of components. n AdjustmentEvent: generated by adjustment of adjustable components such as scroll bars n ContainerEvent: generated when components are added or removed from a container. n FocusEvent: generated when a component receives or loses input focus.

7 The Event Classes n ItemEvent: generated when an is selected from a list, choice, or check box. n KeyEvent: generated by keybord activity. n MouseEvent: generated by mouse activity. n PaintEvent: generated when a component is painted. n TextEvent: generated when a text component is modified. n WindowEvent: generated by window activity (such as iconifying or de-iconifying.)

8 The Event Classes n InputEvent: ä long getWhen(): returns the time when the event took place. n MouseEvent: ä int getX(), getY(): returns the position of the mouse within the originating component at the time the event took place.

9 Event Listeners n An event listener is an object to which a component has delegated the task of handling a particular kind of event. n When the component experiences input, an event of the appropriate type is constructed, the event is then passed as the parameter to a method call on the listener. n A listener must implement the interface that contains the event-handling method.

10 java.awt.event n ActionListener n AdjustmentListener n ComponentListener n ContainerListener n FocusListener n ItemListener

11 java.awt.event n KeyListener n MouseListener n MouseMotionListener n TextListener n WindowListener

12 Event Handling n Register an event listener: Button button = new Button(“1”); ButtonHandler bHandler = new ButtonHandler(); button.addActionListener(bHandler); n Implement an event Handler: private class ButtonHandler implements ActionListener { public void actionPerformed (ActionEvent e ) { if (e.getActionCommand()== "+/-") { do something…. }

13 Adapters n An adapter is simply a class that implements an interface by providing do-nothing methods. public static man(String[] args) { Frame aFrame = new Frame(); Frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit( 0 ); }

14 WindowListener Frame aFrame = new Frame(); Frame.addWindowListener( new MyWindowListener() ); …… } class MyWindowListener implements WindowListener {

15 MyWindowListener { windowActivated(WindowEvent) { } windowClosed(WindowEvent) { } windowClosing (WindowEvent) {System.exit(0);} windowDeactivated (WindowEvent){ } windowDeiconified (WindowEvent){ } windowIconfied (WindowEvent){ } windowOpened (WindowEvent){ }

16 WindowListener n windowActivated(WindowEvent) n windowClosed(WindowEvent) n windowClosing (WindowEvent) n windowDeactivated (WindowEvent) n windowDeiconified (WindowEvent) n windowIconfied (WindowEvent) n windowOpened (WindowEvent)

17 Explict Event Enabling 1.Create a subclass of the component. 2. In the subclass constructor, call enableEvents(AWTEvent.XXX_Event_Mas k). 3. Provide the subclass with a processXXXEvent() method; this method should call the superclass’ version before returning.

18 Explict Event Enabling class MyBtn extends Button { public MyBtn(String label) { super(label); enableEvents(AWTEvent.ACTION_EVENT_MASK); } public void processActionEvent(ActionEvent ae) { System.out.println(“processing an action event”); super.processActionEvent(ae); }


Download ppt "Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event."

Similar presentations


Ads by Google