Chapter 12 Event-Driven Programming


In event-drivent programming, code is executed when an event occurs - such as a button click or a mouse movement. 

Example: Assume program A has a button X.

Events and Event Source

An event is a signal to the program that something has happened.  The program can choose to respond to or ignore the event.  The component on which an event is generated (e.g., a button which is pressed) is called the source object.  The table below shows the types of events generated by various source objects.

User Action
Source Object
Event Type
Click button
JButton
ActionEvent
Press return on text field
JTextField
ActionEvent
Select new item
JComboBox
ItemEvent, ActionEvent
Select item(s)
JList
ListSelectionEvent
Click check box
JCheckBox
ItemEvent, ActionEvent
Click radio button
JRadioButton
ItemEvent, ActionEvent
Select menu item
JMenuItem
ActionEvent
Move scroll bar
JScrollBar
AdjustmentEvent
Window open, close, iconify, deiconify, close
Window
WindowEvent
Component add or remove from container
Container
ContainerEvent
Component moved, resized, hidden or shown
Component
ComponentEvent
Component gained or lost focus
Component
FocusEvent
Key released or pressed
Component
KeyEvent
Mouse pressed, released, clicked, entered or exited
Component
MouseEvent
Mouse moved or dragged
Component
MouseEvent





 An event is an instance of an event class.  The root class of the event clases is java.util.EventObject.  Figure 12.1 in the text shows the hierarchical relationships of the event classes.

An event object contains properties pertinent to the event.  You can identify the source using getSource(). Most event classes are in java.awt.event except ListSelectionEvent, which is in javax.swing.event. 

Listeners, Registrations, and Handling Events

When an event is triggered, an interested object receives the event.  This object is called a listener.  The listener must implement the corresponding event-listener interface.  The naming convention is generally XListener for XEvent, except for MouseMotionListener.  The interface specifies which methods must be implemented. 

Event Class
Listener Interface
Methods (handlers)
ActionEvent
ActionListener
actionPerformed(ActionEvent e)
ItemEvent
ItemListener
itemStateChanged(ItemEvent e)
WindowEvent
WindowListener
windowClosing(WindowEvent e)


windowOpened(WindowEvent e)


windowIconified(WindowEvent e)


windowDeiconified(WindowEvent e)


windowClosed(WindowEvent e)


windowActivated(WindowEvent e)


windowDeactivated(WindowEvent e)
ContainerEvent
ContainerListener
componentAdded(ContainerEvent e)


componentRemoved(ContainerEvent e)
ComponentEvent
ComponentListener
componentMoved(ComponentEvent e)


componentHidden(ComponentEvent e)


componentResized(ComponentEvent e)


componenetShown(ComponentEvent e)
FocusEvent
FocusListener
focusGained(FocusEvent e)


focusLost(FocusEvent e)
TextEvent
TextListener
textValueChanged(TextEvent e)
KeyEvent
KeyListener
keyPressed(KeyEvent e)


keyReleased(KeyEvent e)


keyTyped(KeyEvent e)
MouseEvent
MouseListener
mousePressed(MouseEvent e)


mouseReleased(MouseEvent e)


mouseEntered(MouseEvent e)


mouseExited(MouseEvent e)


mouseClicked(MouseEvent e)

MouseMotionListener
mouseDragged(MouseEvent e)


MouseMoved(MouseEvent e)
AdjustmentEvent
AdjustmenetListener
adjustmentValueChanged(AdjustmentEvent e)




Sample button listener:

class ButtonListener implements ActionListener {
/** This method will be invoked when a button is clicked */
    public void actionPerformed(ActionEvent e) {
       if (e.getActionCommand().equals("OK")) {
          System.out.println("The OK button is clicked");
    }
    else if (e.getActionCommand().equals("Cancel")) {
          System.out.println("The Cancel button is clicked");
     }
  }
}

By default, the action command is the text of the button.  You could also use e.getSource() to return the reference of the source object and determine which button was clicked.

To "register" this class as your listener:
//  create buttons
private JButton jbtOK = new JButton("OK");
private JButton jbtCancel = new JButton("Cancel");
// create button listener
ButtonListener btListener = new ButtonListener();
jbtOk.addActionListener(btListener);
jbtCancel.addActionListener(btListener);

It is also possible for the class itself to implement ActionListener.  In that case, the class should include the appropriate handler method (actionPerformed in this example). To register the handler, the line of code would be:
jbtOk.addActionListener(this);

NOTE: If a listener is registered with a source twice, the handler will be invoked twice when an event occurs.

Mouse Events

A mouse event is generated when a mouse is clicked, released, moved or dragged on a component.  The event captures the location (x and y coordinate) and the number of clicks.  Java provides two listener interfaces for mouse events: MouseListener and MouseMotionListener. 


Keyboard Events

Keyboard events capture input from the keyboard.  Every event has an associated key character or key code that is returned by the getKeyChar() or getKeyCode method in KeyEvent.