Docsity

GUI Event Handling - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download GUI Event Handling - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

GUI Event Handling

What is an Event?

  • GUI components communicate with the rest of the applications through events.
  • The source of an event is the component that causes that event to occur.
  • The listener of an event is an object that receives the event and processes it appropriately.

An example of Event Handling

public class SwingApplication implements ActionListener { ... JButton button = new JButton("I'm a Swing button!"); button.addActionListener(this); .... public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); } }

The Event Handling process

  • When an event is triggered, the JAVA runtime first determines its source and type.
  • If a listener for this type of event is registered with the source, an event object is created.
  • For each listener to this type of an event, the JAVA runtime invokes the appropriate event handling method to the listener and passes the event object as the parameter.

What does an Event Handler

require?

  • It just looks for 3 pieces of code!
  • First, in the declaration of the event handler class, one line of code must specify that the class implements either a listener interface or extends a class that implements a listener interface. public class DemoClass implements ActionListener {

What does an Event Handler

require? (contd..)

  • Second, it looks for a line of code which registers an instance of the event handler class as a listener of one or more components because, as mentioned earlier, the object must be registered as an event listener. anyComponent.addActionListener(instanceOf DemoClass);

Types of Events

  • Below, are some of the many kinds of events, swing components generate. Act causing Event Listener Type User clicks a button, presses Enter, typing in text field

ActionListener

User closes a frame WindowListener Clicking a mouse button, while the cursor is over a component

MouseListener

Types of Events (contd..)

Act causing Event Listener Type

User moving the mouse over a component

MouseMotionListener

Component becomes visible ComponentListener

Table or list selection changes ListSelectionListener

Event Listeners

  • Event listeners are the classes that implement the Listener interfaces. Example:
    1. ActionListener receives action events
    2. MouseListener receives mouse events. The following slides give you a brief overview on some of the listener types.

The ActionListener Method

  • It contains exactly one method.

Example: public void actionPerformed(ActionEvent e) The above code contains the handler for the ActionEvent e that occurred.

The MouseListener Methods

(contd..)

  • Event handling when the mouse button is pressed on a component. public void mousePressed(MouseEvent e)
  • Event handling when the mouse button is released on a component. public void mouseReleased(MouseEvent e)

The MouseMotionListener

Methods

  • Invoked when the mouse button is pressed over a component and dragged. Called several times as the mouse is dragged public void mouseDragged(MouseEvent e)
  • Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed. public void mouseMoved(MouseEvent e)

The WindowListener Methods

(contd..)

  • Invoked when the window object is closed as a result of calling dispose (release of resources used by the source). public void windowClosed(WindowEvent e)
  • Invoked when the window is set to be the active window. public void windowActivated(WindowEvent e)

The WindowListener Methods

(contd..)

  • Invoked when the window object is no longer the active window public void windowDeactivated(WindowEvent e)
  • Invoked when the window is minimized. public void windowIconified(WindowEvent e)
  • Invoked when the window is changed from the minimized state to the normal state. public void windowDeconified(WindowEvent e)