JAVA EventHandling Prepared by Miss. Arati A. Gadgil
EventHandling Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. Foreground Events - Those events which require the direct interaction of user Background Events - Those events that require the interaction of end user are known as background events 2
Source - The source is an object on which event occurs Listener - It is also known as event handler. Listener is responsible for generating response to an event. Steps involved in event handling •The User clicks the button and the event is generated. •Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. •Event object is forwarded to the method of registered listener class. •The method is now get executed and returns. 3
4 Event Class Hierarchy Each high level event class extends java.awt.AWTEvent. java.lang.Object | ---java.util.EventObject | ---java.awt.AWTEvent | ---java.awt.event.ActionEvent | ---java.awt.event.ItemEvent | ---java.awt.event.AdjustmentEvent | ---java.awt.event.TextEvent | ----java.awt.event.ComponentEvent | ---java.awt.event.InputEvent | | | ---java.awt.event.KeyEvent | | ---java.awt.event.MouseEvent | +---java.awt.event.FocusEvent | +---java.awt.event.ContainerEvent | +---java.awt.event.WindowEvent
5 Semantic and Low-Level Events in the AWT A semantic event is one that expresses what the user is doing, such as "clicking that button"; hence, an ActionEvent is a semantic event. Low-level events are those events that make this possible. In the case of a button click, this is a mouse down, a series of mouse moves, and a mouse up (but only if the mouse up is inside the button area). Or it might be a keystroke, which happens if the user selects the button with the TAB key and then activates it with the space bar. Similarly, adjusting a scrollbar is a semantic event, but dragging the mouse is a low-level event. Semantic event classes in the java.awt.event package: ActionEvent (for a button click, a menu selection, selecting a list item, or ENTER typed in a text field) AdjustmentEvent (the user adjusted a scrollbar) ItemEvent (the user made a selection from a set of checkbox or list
6 Five low-level event classes are commonly used: •KeyEvent (a key was pressed or released) •MouseEvent (the mouse button was pressed, released, moved, or dragged) •MouseWheelEvent (the mouse wheel was rotated) •FocusEvent (a component got focus, or lost focus). See page 321 for more information about the focus concept. •WindowEvent (the window state changed)
AWT ActionListener Interface Method void actionPerformed(ActionEvent e) Invoked when an action occurs. The Event listener represent the interfaces responsible to handle events. 7
AWT MouseListener Interface Methods •void mouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed and released) on a component. •void mouseEntered(MouseEvent e) Invoked when the mouse enters a component •void mouseExited(MouseEvent e) Invoked when the mouse exits a component. •void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component. •void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component. . 8
AWT WindowListener Interface Methods void windowActivated(WindowEvent e) Invoked when the Window is set to be the active Window void windowClosed(WindowEvent e) Invoked when a window has been closed as the result of calling dispose on the window. void windowClosing(WindowEvent e) Invoked when the user attempts to close the window from the window's system menu. void windowDeactivated(WindowEvent e) Invoked when a Window is no longer the active Window 9
void windowDeiconified(WindowEvent e) Invoked when a window is changed from a minimized to a normal state void windowIconified(WindowEvent e) Invoked when a window is changed from a normal to a minimized state. void windowOpened(WindowEvent e) Invoked the first time a window is made visible. 10
import java.awt.*; import java.awt.event.*; class frmw extends Frame implements WindowListener { frmw() { super("window Listener"); addWindowListener(this); setSize(300,300); setVisible(true); } public void windowActivated(WindowEvent e) { System.out.println("Activated"); } 11
public void windowDeactivated(WindowEvent e) { System.out.println("Deactivated"); } public void windowIconified(WindowEvent e) { System.out.println("Iconified"); } public void windowDeiconified(WindowEvent e) { System.out.println("Deiconified"); } public void windowOpened(WindowEvent e) { System.out.println("opened"); } 12
public void windowClosed(WindowEvent e) { System.out.println("closed"); } public void windowClosing(WindowEvent e) { System.out.println("closing"); } public static void main(String []a) { frmw k=new frmw(); } } 13
AWT MouseMotionListener Interface Methods void mouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged void mouseMoved(MouseEvent e) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed 14
AWT FocusListener Interface Methods void focusGained(FocusEvent e) Invoked when a component gains the keyboard focus void focusLost(FocusEvent e) Invoked when a component loses the keyboard focus 15
Thank You 16

Java eventhandling

  • 1.
  • 2.
    EventHandling Change in thestate of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. Foreground Events - Those events which require the direct interaction of user Background Events - Those events that require the interaction of end user are known as background events 2
  • 3.
    Source - Thesource is an object on which event occurs Listener - It is also known as event handler. Listener is responsible for generating response to an event. Steps involved in event handling •The User clicks the button and the event is generated. •Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. •Event object is forwarded to the method of registered listener class. •The method is now get executed and returns. 3
  • 4.
    4 Event Class Hierarchy Eachhigh level event class extends java.awt.AWTEvent. java.lang.Object | ---java.util.EventObject | ---java.awt.AWTEvent | ---java.awt.event.ActionEvent | ---java.awt.event.ItemEvent | ---java.awt.event.AdjustmentEvent | ---java.awt.event.TextEvent | ----java.awt.event.ComponentEvent | ---java.awt.event.InputEvent | | | ---java.awt.event.KeyEvent | | ---java.awt.event.MouseEvent | +---java.awt.event.FocusEvent | +---java.awt.event.ContainerEvent | +---java.awt.event.WindowEvent
  • 5.
    5 Semantic and Low-LevelEvents in the AWT A semantic event is one that expresses what the user is doing, such as "clicking that button"; hence, an ActionEvent is a semantic event. Low-level events are those events that make this possible. In the case of a button click, this is a mouse down, a series of mouse moves, and a mouse up (but only if the mouse up is inside the button area). Or it might be a keystroke, which happens if the user selects the button with the TAB key and then activates it with the space bar. Similarly, adjusting a scrollbar is a semantic event, but dragging the mouse is a low-level event. Semantic event classes in the java.awt.event package: ActionEvent (for a button click, a menu selection, selecting a list item, or ENTER typed in a text field) AdjustmentEvent (the user adjusted a scrollbar) ItemEvent (the user made a selection from a set of checkbox or list
  • 6.
    6 Five low-level eventclasses are commonly used: •KeyEvent (a key was pressed or released) •MouseEvent (the mouse button was pressed, released, moved, or dragged) •MouseWheelEvent (the mouse wheel was rotated) •FocusEvent (a component got focus, or lost focus). See page 321 for more information about the focus concept. •WindowEvent (the window state changed)
  • 7.
    AWT ActionListener Interface Method voidactionPerformed(ActionEvent e) Invoked when an action occurs. The Event listener represent the interfaces responsible to handle events. 7
  • 8.
    AWT MouseListener Interface Methods •voidmouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed and released) on a component. •void mouseEntered(MouseEvent e) Invoked when the mouse enters a component •void mouseExited(MouseEvent e) Invoked when the mouse exits a component. •void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component. •void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component. . 8
  • 9.
    AWT WindowListener Interface Methods voidwindowActivated(WindowEvent e) Invoked when the Window is set to be the active Window void windowClosed(WindowEvent e) Invoked when a window has been closed as the result of calling dispose on the window. void windowClosing(WindowEvent e) Invoked when the user attempts to close the window from the window's system menu. void windowDeactivated(WindowEvent e) Invoked when a Window is no longer the active Window 9
  • 10.
    void windowDeiconified(WindowEvent e) Invokedwhen a window is changed from a minimized to a normal state void windowIconified(WindowEvent e) Invoked when a window is changed from a normal to a minimized state. void windowOpened(WindowEvent e) Invoked the first time a window is made visible. 10
  • 11.
    import java.awt.*; import java.awt.event.*; classfrmw extends Frame implements WindowListener { frmw() { super("window Listener"); addWindowListener(this); setSize(300,300); setVisible(true); } public void windowActivated(WindowEvent e) { System.out.println("Activated"); } 11
  • 12.
    public void windowDeactivated(WindowEvente) { System.out.println("Deactivated"); } public void windowIconified(WindowEvent e) { System.out.println("Iconified"); } public void windowDeiconified(WindowEvent e) { System.out.println("Deiconified"); } public void windowOpened(WindowEvent e) { System.out.println("opened"); } 12
  • 13.
    public void windowClosed(WindowEvente) { System.out.println("closed"); } public void windowClosing(WindowEvent e) { System.out.println("closing"); } public static void main(String []a) { frmw k=new frmw(); } } 13
  • 14.
    AWT MouseMotionListener Interface Methods voidmouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged void mouseMoved(MouseEvent e) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed 14
  • 15.
    AWT FocusListener Interface Methods voidfocusGained(FocusEvent e) Invoked when a component gains the keyboard focus void focusLost(FocusEvent e) Invoked when a component loses the keyboard focus 15
  • 16.