Monday 26 March 2012

Event Handling Basics

Event:
•An event is an object which describes a state change in a source.
•Events are generally generated when a user interacts with the GUI.
•Some examples of user generated events are: mouse click, key press, button click etc…
•Some example of non-user generated events are: timeouts, h/w failures, interrupts etc…
•Events are reflected as classes in Java.

Event Source:
•A source is an object on which events are generated.
•A source can generate more than one event.
•When an event is generated, there is some change in the state of the event.
•A source must register with a listener, for the events to be detected.
•When a listener detects an event, it passes the event to an event handler.
•Examples of event sources are: button, textbox, list box, drop down box, scrollbars, checkbox, radiobuttons etc…

Event Listeners:
•A listener is an object which is notified when an event occurs.
•There are two major requirements that must be satisfied by a listener:
1) It must register with one or more sources to receive events
2) It must provide event handlers for handling the events.
•Listeners are reflected as interfaces in Java.

Event Classes:
•In Java, event classes are the corner stones in event handling.
•For every event, there is a predefined class in Java.
•The root class for all the event classes in Java is “EventObject”
•Almost all the event classes are available in the “java.awt.event” package.
•The root class for all the event classes in “java.awt” package is “AWTEvent”.


•The event class hierarchy is as shown below:
•Some of the event classes in Java are:
 
•Following are some of the GUI components that generate events:
•The event listeners are defined as interfaces in Java and are available in “java.awt.event” package.
•Some of the event listener interfaces are shown below:
Event Listeners:

•The ActionListener Interface:
This interface defines the actionPerformed( ) method that is invoked when an action event occurs. Its general form is shown here:
void actionPerformed(ActionEvent ae)

•The AdjustmentListener Interface:
This interface defines the adjustmentValueChanged( ) method that is invoked when an adjustment event occurs. Its general form is shown here:
void adjustmentValueChanged(AdjustmentEvent ae)

•The ComponentListener Interface:
This interface defines four methods that are invoked when a component is resized, moved, shown, or hidden. Their general forms are shown here:
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)

•The ContainerListener Interface:
This interface contains two methods. When a component is added to a container,
componentAdded( ) is invoked. When a component is removed from a container,
componentRemoved( ) is invoked. Their general forms are shown here:
void componentAdded(ContainerEvent ce)
void componentRemoved(ContainerEvent ce)

•The FocusListener Interface:
This interface defines two methods. When a component obtains keyboard focus, focusGained( ) is invoked. When a component loses keyboard focus, focusLost( ) is called. Their general forms are shown here:
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)

•The ItemListener Interface:
This interface defines the itemStateChanged( ) method that is invoked when the state of an item changes. Its general form is shown here:
void itemStateChanged(ItemEvent ie)

•The KeyListener Interface:
This interface defines three methods. The keyPressed( ) and keyReleased( ) methods are invoked when a key is pressed and released, respectively. The keyTyped( ) method is invoked when a character has been entered.
The general forms of these methods are shown here:
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)

•The MouseListener Interface:
This interface defines five methods. If the mouse is pressed and released at the same point, mouseClicked( ) is invoked. When the mouse enters a component, the mouseEntered( ) method is called. When it leaves, mouseExited( ) is called. The mousePressed( ) and mouseReleased( ) methods are invoked when the mouse is pressed and released, respectively.
The general forms of these methods are shown here:
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)

•The MouseMotionListener Interface:
This interface defines two methods. The mouseDragged( ) method is called multiple times as the mouse is dragged. The mouseMoved( ) method is called multiple times as the mouse is moved. Their general forms are shown here:
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)

•The MouseWheelListener Interface:
This interface defines the mouseWheelMoved( ) method that is invoked when the mouse wheel is moved. Its general form is shown here:
void mouseWheelMoved(MouseWheelEvent mwe)

•The TextListener Interface:
This interface defines the textChanged( ) method that is invoked when a change occurs in a text area or text field. Its general form is shown here:
void textChanged(TextEvent te)

•The WindowFocusListener Interface:
This interface defines two methods: windowGainedFocus( ) and windowLostFocus( ). These are called when a window gains or loses input focus. Their general forms are shown here:
void windowGainedFocus(WindowEvent we)
void windowLostFocus(WindowEvent we)

•The WindowListener Interface:
This interface defines seven methods. The windowActivated( ) and windowDeactivated( ) methods are invoked when a window is activated or deactivated, respectively. If a window is iconified, the windowIconified( ) method is called. When a window is deiconified, the windowDeiconified( ) method is called. When a window is opened or closed, the windowOpened( ) or windowClosed( ) methods are called, respectively. The windowClosing( ) method is called when a window is being closed. The general forms of these methods are:
void windowActivated(WindowEvent we)
void windowClosed(WindowEvent we)
void windowClosing(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowIconified(WindowEvent we)
void windowOpened(WindowEvent we)

No comments:

Post a Comment