Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.

Similar presentations


Presentation on theme: "Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is."— Presentation transcript:

1 Event Handling

2 The signals that a program receives from the operating system as a result of the actions are called events. A window based program is called an event driven program because the sequence of events are created as a result of the interaction with the GUI which determines what happens in the program.

3 Keystrokes, Mouse Actions Operating System Java Library Classes Method Program Event

4 Java Event Delegation Model In java events are objects. The java.awt.event package defines hierarchy of event types. When an event occurs the programmer can Ignore the event Handle the event by the component where the event originated Delegate event handling to some other object or objects, called listeners.

5 Ignoring the event Do nothing

6 Handling the events in the originating components A self-contained component is the one that handles the event that it generates. For a component to handle its own events, a subclass needs to be created. The subclass needs to do two things: Get ready to receive events, by calling its enableEvents() method. Implement a processActionEvent() method which will be called when an event is received.

7 The class AWTEvent is a subclass of java.util.EventObject. It defines a method getSource() which returns the object that is the source of an event as type Object. The class AWTEvent defines constants which are public final whose values identifying the various kinds of events. MOUSE_EVENT_MASK KEY_EVENT_MASK ITEM__EVENT_MASK WINDOW_EVENT_MASK MOUSE_MOTION_EVENT_MASK FOCUS_EVENT_MASK TEXT_EVENT_MASK ADJUSTMENT_EVENT_MASK Each having of single bit. They can be combined by using bitwise OR operator.

8 Enabling Events enalbleEvent() is inherited by component class so any component can handle its own events. Pass an argument defining the events which the component has to handle. More than one events can be combined with bitwise OR opertaor ( | ). E.g. a window that handles both mouse events and window events enableEvents( AWTEvent.WINDOW_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK );

9 When an event is generated, the JVM generates an “action event” and then checks whether ActionEvent() is enabled. If so then JVM calls processActionEvent() which takes one argument as the object of ActionEvent class. Example

10 By passing different values to enableEvents(), a component can catch many different kinds of events. All of the event handlers have names of the format processXXXEvent(), where XXX stands for the event type.

11 Delegating the event An event always has a source object e.g. Button. When the button is clicked, it’ll create a new object that represents and identifies this event. This object will contain information about the event and its source.

12 Button clicked OK creates ActionEvent Object public class ActionHandler Implements ActionListener{ //class member //constructor….. …… button.addActionListener(this); ……….. ……… void actionPerformed(ActionEvent e) { //handle the event ……. } The listener connects to the event source by calling addActionListener() method for the source object Passed To

13 How to define a listener? Variety of listener interfaces are available for different kinds of events. InterfaceInterface Methods AddMethod ActionListeneractionPerformed(ActionEvent) addActionListener() AdjustmentListeneradjustmentValueChanged (AdjustmentEvent) addAdjustmentListener() ComponentListenercomponentHidden(ComponentEvent) componentMoved(ComponentEvent) componentResized(ComponentEvent) addComponentListener() componentShown(ComponentEvent) ContainerListenercomponentAdded(ContainerEvent) componentRemoved(ContainerEvent) addContainerListener() FocusListenerfocusGained(FocusEvent) focusLost(focusEvent) addFocusListener() ItemListeneritemStateChanged(ItemEvent) addItemListener() KeyListenerkeyPressed(keyEvent) keyReleased(keyEvent) keyTyped(keyEvent) addKeyListener()

14 MouseListenermouseClicked(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mousePressed(MouseEvent) mouseReleased(MouseEvent) addMouseListener() MouseMotionListenermouseDragged(MouseEvent) mouseMoved(MouseEvent) addMouseMotionListener() TextListenertextValueChanged(TextEvent) addTextListener() WindowListenerwindowActivated(WindowEvent) windowClosed(WindowEvent) windowClosing(WindowEvent) windowDeactivated(WindowEvent) windowDeiconified(WindowEvent) windowIconified(WindowEvent) windowOpened(WindowEvent) addWindowListener()

15 The steps to support event handling is as follows: Create a listener class that implements the desired listener interface. Construct the component. Construct an instance of the listener class. Call addXXXListener() on the component, passing in the listener object. Example

16 Java Event Type ( java.awt.event) java.util.EventObject java.awt.Awt.Event ActionEvent AdjustmentEventItemEvent TextEvent ComponentEvent ContainerEvent FocusEvent PaintEvent WindowEvent InputEvent MouseEvent KeyEvent

17 Java.util.EventObject provides a method getSource() which returns the component in which the event took place. Java.awt.AWTEvent provides a method getID() which returns an int value that describes the nature of event e.g. getID for MouseEvent results in int value of MouseEvent.Mouse_Pressed, MouseEvent.Mouse_Dragged, etc.

18 Adjustment Events Sent by scrollbars. public class AdjustmentEvent extends AWTEvent { public static final int BLOCK_DECREMENT; public static final int BLOCK_INCREMENT; public static final int TRACK; public static final int UNIT_DECREMENT; public static final int UNIT_INCREMENT; public AdjustmentEvent(Adjustable s, int id, int type, int value); public Adjustable getAdjustable(); public int getAdjustmentType(); public int getValue(); public String paramString(); } public interface AdjustmentListener extends EventListener { public void adjustmentValueChanged(AdjustmentEvent AdjEvt); } Example

19 Item Events Generated by components that present users with items to choose from. The components are- Choice, List, Checkbox, CheckboxMenuItem.

20 public static final int DESELECTED; public static final int ITEM_STATE_CHANGED; public static final int SELECTED; public ItemEvent(ItemSelectable src, int id, Object item, int stateChanged); public Object getItem(); public ItemSelectable getItemSelectable(); public int getStateChange(); public String paramString();

21 A component can process its own item events by calling enableEvents(AWTEvent.ITEM_EVENT_MASK) and providing a processItemEvent() method. OR Can delegate by- public interface ItemListener extends EventListener { void itemStateChanged(ItemEvent e); } Example

22 Key Events Key events are generated when a user presses or releases a key. KEY_PRESSED indicates that a key was pushed down. KEY_RELEASED indicates that a key was released. KEY_TYPED denotes a key press followed by a key release.

23 public static String getKeyText(int keyCode); public char getKeyChar(); public int getKeyCode(); public boolean isActionKey(); public void setKeyCode(int keyCode); It handles own event by enableEvents(AWTEvent.KEY_EVENT_MASK) and processKeyEvent() OR delegate by- public interface KeyListener extends EventListener { public void keyPressed(KeyEvent k); public void keyReleased(KeyEvent k); public void keyTyped(KeyEvent k); }

24 Mouse Events public static final int MOUSE_DRAGGED; public static final int MOUSE_ENTERED; public static final int MOUSE_EXITED; public static final int MOUSE_MOVED; public static final int MOUSE_PRESSED; public static final int MOUSE_RELEASED;

25 public Point getPoint(); public int getX(); public int getY(); A mouse event is processed by calling enableEvents(AWTEvent.MOUSE_EVENT_ MASK) and processMouseEvent(). OR event can be delegated to listener by implementing MouseListener interface.

26 public interface MouseListener extends EventListener { public void mouseClicked(MouseEvent m); public void mouseEntered(MouseEvent m); public void mouseExited(MouseEvent m); public void mousePressed(MouseEvent m); public void mouseReleased(MouseEvent m); }

27 Mouse-motion events are handled by- enableEvents(AWTEvent.MOUSE_MOTION_EVENT _MASK) and processMouseMotionEvent() OR delegate by public interface MouseMotionListener extends EventListener { public void mouseDragged(MouseEvent m); public void mouseMoved(MouseEvent m); }

28 Window Events public class WindowEvent extends ComponentEvent { public static final int WINDOW_ACTIVATED; public static final int WINDOW_CLOSED; public static final int WINDOW_CLOSING; public static final int WINDOW_DEACTIVATED; public static final int WINDOW_DEICONIFIED; public static final int WINDOW_ICONIFIED; public static final int WINDOW_OPENED; public WindowEvent(Window src, int id); public Window getWindow(); public String paramStrings(); }

29 Adapters An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. class MyMouse implements MouseListener { public void mouseClicked(MouseEvent me) { } } This class won't compile because you haven't declared the other methods of the interface; mouseEntered(MouseEvent), mouseExited(MouseEvent), etc... So you use an adapter: class MyMouse extends MouseAdapter { public void mouseClicked(MouseEvent me) { } } The compiler won't complain now because the superclass MouseAdapter already provides do-nothing methods for the interface implementation.

30 Adapter ClassListener Interface ComponentAdapterComponentListener ContainerAdapter ContainerListener FocusAdapterFocusListener KeyAdapter KeyListener MouseAdapterMouseListener MouseMotionAdapterMouseMotionListener WindowAdapterWindowListener

31 Example


Download ppt "Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is."

Similar presentations


Ads by Google