|
4.6 Event Listeners
EventListeners are used to capture actions performed with GUI components like Buttons, Lists, Checkboxes, etc. Simply by adding GUI components to a layout does not enable their intended use. A button, even though it may appear to have some interactive behavior when clicking on it, does not register the clicking action unless an EventListener has been specifically set up to "listen" to its action.
EventListeners are added to GUI components, so that actions performed on them trigger some method that handles the particular action. There are a number of EventListeners for the different standard GUI components:
-
ActionListener: notifies of Button clicks, selections in List, and mouse clicks in TextField
-
TextListener: notifies of text changes in TextField and TextArea
-
AdjustmentListener: notifies of value changes in Scrollbar,
-
ItemListener: notifies of value changes in components List, Choice, and Checkbox
All events are implemented as interfaces, i.e. polymorphism is used. One of the benefits of this design choice is that it is now possible to implement any number of different interfaces, depending on what event listeners the applet requires. Custom-designed event handlers can also be implemented with this scheme.
Adding
EventListeners for a given GUI component to an applet requires 3 steps:
- Use the keyword
implements
to include the interface in the class
- Use a method in the GUI component to add the implementing class to the list of classes which are notified upon performed actions
- Include and implement the method or methods that are required for the interface
4.6.1 Including the interface
An event listener is added to an applet by implementing the listener, for example, and ActionListener:
public class someActionApplet implements ActionListener { ... }
|
or, when several event listeners are used:
public class someActionApplet implements ActionListener, AdjustmentListener, ItemListener { ... }
|
Most EventListeners are part of a package that needs to be imported, e.g.:
4.6.2 Registering the EventListener
To register a class with an
EventListener, the GUI component in question must be made aware that its actions are to invoke a handling method. The following line is used to merely add a Button to a container:
Button button = new Button("Some Button"); add(button);
|
However, this code does not enable this button to notify the implementing class of its actions. To this end, the following amendment is necessary:
button.addActionListener(this);
|
The class Button includes the method addActionListener(...) and requires an argument that points to the class that will eventually handle the event. In most cases, that is that same class, and hence the keyword this is used.
4.6.3 Implement Event Method
Finally, the class must also implement the method prototypes defined in the individual interfaces, in order to invoke the correct method for handling the event. In case of an ActionListener, the associated method is:
public void actionPerformed(ActionEvent e) { ... }
|
The code in this method can now handle an ActionEvent, such as a button press. The passed object ActionEvent contains some information about the origin of the action. There is no necessity to use this object, but it can be helpful in deciding between different actions. See example below.
4.6.4 Example
import java.applet.*; import java.awt.*; import java.awt.event.*;
public class ButtonEventApplet extends Applet implements ActionListener {
Button button;
public void init() {
setLayout(new BorderLayout());
button = new Button("Click Me!"); button.addActionListener(this); add(button, BorderLayout.CENTER); }
public void actionPerformed(ActionEvent e) {
// do something when the button is pressed button.setText("Clicked"); } }
|
In this example, we follow the three requirements for adding an EventListener:
- we implement
ActionListener using ... extends Applet implements ActionListener { ...
- we build a Button GUI component, and register the event with the button:
b1.addActionListener(this);
- we implement the method that is invoked when a Button action takes place:
public void actionPerformed(ActionEvent e) { ... }
4.6.5 Multiple components with same EventListener
Since an applet may have multiple buttons, each Button event arrives at the same
public void actionPerformed(..)
method, and each time a new and different ActionEvent object is passed along as an argument. This object can be used to distinguish between the source of the action, i.e. which button was pressed to create the current ActionEvent. A very useful method in ActionEvent is called getSource()
. This method returns a reference to the object on which an action has been performed. If the buttons in the applet are declared globally, comparisons can be made to determine which button caused the action:
import java.applet.*; import java.awt.*; import java.awt.event.*;
public class MultipleActionApplet extends Applet implements ActionListener {
Button b1, b2, b3;
public void init() {
setLayout(new FlowLayout());
b1 = new Button("Click Me!"); b1.addActionListener(this); add(b1);
b2 = new Button("No no, Click ME!"); b2.addActionListener(this); add(b2);
b3 = new Button("Don't listen to them! Click Me instead!"); b3.addActionListener(this); add(b3); }
public void actionPerformed(ActionEvent e) {
Object source = e.getSource(); if (source == b1) { // Button 1 was pressed: do something b3.setEnabled(true); b2.setEnabled(false); } else if (source == b2) { // Button 2 was pressed: do something b1.setEnabled(true); b3.setEnabled(false); } else if (source == b3) { // Button 3 was pressed: do something b1.setEnabled(false); b2.setEnabled(true); } } }
|
In this example, we compare the source of the ActionEvent to all of the defined buttons, and depending on the match, some actions are performed.
Examples for other GUI components and their
EventListeners follow.
Checkboxes use the ItemListener to detect changes in state of a checkbox: checked or unchecked.
TextFields use the ActionListener to detect Enter-button keystrokes.
Choices use the ItemListener to detect changes in selection of items.
Lists use the ItemListener to detect changes in selection of items, as well as the ActionListener to detect double-clicks on any items.
Scrollbars use the AdjustmentListener to detect changes in value, i.e. changes in location of the slider.
|