Event driven programming

Event driven programming separates things that happen from how they’re handled. Each object is free to implement the same event handler in a different, customized way. In Java’s AWT, certain GUI events are automatically dispatched by the Java runtime.

Procedural Programming vs. Event-Driven Programming

  • Procedural programming is executed in procedural order.
  • In event-driven programming, code is executed upon activation of events.

Events

An event can be defined as a type of signal to the program that something has happened.

The event is generated by

  • External user actions
  • e.g. mouse movements, mouse clicks, and keystrokes
  • the operating system
  • e.g. a Timer

Event Object

An event object contains whatever properties that are relevant to the event.

To identify the source object of the event:

  • use the getSource() instance method in the EventObject class.
  • A component on which an event is generated is called the source object.
  • All the event classes are subclasses of EventObject.
  • If a component can generate an event, any subclass of the component can generate the same type of event.

The subclasses of EventObject deal with special types of events, such as button actions, window events, component events, mouse movements, and keystrokes.

Every GUI component can generate MouseEvent, KeyEvent, FocusEvent, and ComponentEvent.

The Delegation Model

  • A listener may listen for multiple sources.
  • The listener objectʹs class must implement the corresponding event-listener interface.
  • The listener object must be registered by the source object.
  • A source may have multiple listeners.
  • Each event class has a corresponding listener interface.
1
2
3
JButton jbt = new JButton("OK"); 
ActionListener listener = new OKListener();
jbt.addActionListener(listener);

Event Handlers

The listenerʹs ________ method is invoked after a mouse button is released?

  • public void mouseClicked(MouseEvent e) and public void mouseReleased(MouseEvent e)

Inner Class

A Inner class is a member of another class.

In some applications, you can use an inner class to make programs simple.

An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class.

  • Inner Class can access private instance variables in the enclosing object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// OuterClass.java: inner class demo
public class OuterClass {
private int data;

/** A method in the outer class */
public void m() {
// Do something
}

// An inner class
class InnerClass {
/** A method in the inner class */
public void mi() {
// Directly reference data and method
// defined in its outer class
data++;
m();
}
}
}

A listener class is designed specifically to create a listener object for a GUI component (e.g., a button).

  • It will not be shared by other applications.
  • So, it is appropriate to define the listener class inside the frame class as an inner class.

Anonymous Inner Classes

An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause.

  • An anonymous inner class must implement all the abstract methods in the superclass or in the interface.
  • Inner class listeners can be shortened using anonymous inner classes.
  • An anonymous inner class is an inner class without a name.
  • It combines declaring an inner class and creating an instance of the class in one step.

An anonymous inner class is declared as follows:

1
2
3
4
new SuperClassName/InterfaceName() {
// Implement or override methods in superclass or interface
// Other methods if necessary
}

The interface ActionListener should be implemented to listen for a button action event.

Handling Window Events

Any subclass of the Window class can generate the following window events:

  • window opened,
  • closing,
  • closed,
  • activated,
  • deactivated,
  • iconified, and
  • deiconified.

Handling Mouse Events

Java provides two listener interfaces, MouseListener and MouseMotionListener, to handle mouse events.

  • The MouseListener listens for actions such as when the mouse is
    • pressed,
    • released,
    • entered,
    • exited, or
    • clicked.
  • The MouseMotionListener listens for actions such as
    • dragging or
    • moving the mouse.

Handling Keyboard Events

To process a keyboard event, use the following handlers in the KeyListener interface:

  • keyPressed(KeyEvent e) is called when a key is pressed.
  • keyReleased(KeyEvent e) is called when a key is released.
  • keyTyped(KeyEvent e) is called when a key is pressed and then released.
    • When a unicode is entered

E.g. To check whether a DELETE key is pressed or released,

we can only use keyPressed(KeyEvent e) or keyReleased(KeyEvent e).

The KeyEvent Class

Methods:

  • getKeyChar()
  • getKeyCode()

Key Constants

  • VK_HOME: the Home key
  • VK_END: the End key
  • VK_PGUP: the Page Up key
  • VK_PGDN: the Page Down key

The Timer Class

Some non-GUI components can fire events.

The javax.swing.Timer class is a source component that fires an ActionEvent at a predefined rate.

  • You must always specify a listener when creating a Timer object
  • You can add multiple listeners for a Timer object
  • You can specify a delay using the setDelay method.