At the moment we have displayed components, adding them to applications, and they work to
the extent that you can press the buttons, select from the list etc., however they do not
currently interact with your own code. If we want to use these components we must be aware
of how the java.awt.event
package classes work.
Note that with events the structure is quite different from Java 1.1.x (Java 1) to
Java 1.2+.x (Java 2). There are many reasons for this, but the main reason is that there were
serious problems with the event structure of Java 1, especially in relation to threaded
applications. We are only examining the event structure of Java 2+.
The event structure for the components we have just discussed is very similar to the
event structure that we discussed for the Mouse using the MouseListener
interface in the section called “Using the Mouse in Applets”.
An ActionEvent
object is generated by several of the components we
have just discussed. A component such as a Button
object generates an
ActionEvent
object when it is pressed. As with the
MouseListener
, when we had to use this.addMouseListener(this)
to register the listener, we have to do the same for ActionEvent
s. We register
the listener for any component using, for example:
Button b = new Button("Test Button"); b.addActionListener(this);
Where this
refers to an object that is capabable of handling the
ActionEvent
object that is passed to it. So in this case the class in which
the Button
b is defined must implement that ActionListener
interface. So for a full example, use the following application:
The applet should display which button is pressed in the TextField
as soon
as the button is pressed. The source code for this example is below and in
ButtonEvents.java
. You can see it
working here - ButtonEvents.html
.
1 2 // A Button Events Application 3 4 import java.applet.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 8 public class ButtonEvents extends Applet implements ActionListener 9 { 10 11 private Button button1, button2, button3; 12 private TextField status; 13 14 public void init() 15 { 16 status = new TextField(20); 17 18 this.button1 = new Button("Button 1"); 19 this.button2 = new Button("Button 2"); 20 this.button3 = new Button("Button 3"); 21 22 this.button1.addActionListener(this); 23 this.button2.addActionListener(this); 24 this.button3.addActionListener(this); 25 26 this.add(status); 27 this.add(button1); 28 this.add(button2); 29 this.add(button3); 30 } 31 32 public void actionPerformed(ActionEvent e) 33 { 34 if (e.getActionCommand().equals("Button 1")) 35 { 36 status.setText("Button 1 Pressed"); 37 } 38 else if (e.getActionCommand().equals("Button 2")) 39 { 40 status.setText("Button 2 Pressed"); 41 } 42 else 43 { 44 status.setText("Button 3 Pressed"); 45 } 46 } 47 } 48 49
Some notes about this code segment:
The java.awt.event
package must be included to allow
for the use of the ActionEvent
and associated classes.
The applet must implement ActionListener
to handle
ActionEvent
s. Once our class uses the statement implements
ActionListener
then we must write a method
public void actionPerformed(ActionEvent e)
. If we do not write this method
then the code will not compile.
We must use the method addActionListener()
to the
component to state that this component generates events and to direct these events to a suitable
object. In this applet we use the keyword this
which represents this object
of the applet. So we are sending the event to this
object. We are
only allowed to do that because this
object
implements
ActionListener
. If it did not then an
error would be generated at compile time.
Once the event is sent to this
object then the
actionPerformed()
method is called and it is passed
an ActionEvent
object - in this case called e
. We can then interrogate this
ActionEvent
object to determine its source. In this applet we have
three different buttons that could have caused this object to be generated. One way of
interrogating the ActionEvent
object is to use its
getActionCommand()
method that returns the action command as a
String
object. In the API documentation it states that the action
command for a Button
object is the text on the surface of the button
by default. We then use the equals()
method of the String
class that allows us to compare the action command to a String
such as
"Button 1"
. If the "Button 1"
String
is a perfect match then we set the text of the TextField
to be
"Button 1 Pressed"
using the setText()
method of the
TextField
object status
.
The ActionEvent
class and the ActionListener
interface
mechanism can be used to deal with events on buttons and on other components also. The
ActionEvent
class deals with actions that can occur. Other events can be created by
other components - how do we know which events are created by which components? Well you have to read the API
documentation for that component. For the components we have looked at in this section:
Button
objects generate an ActionEvent
object when the mouse is
pressed on the Button
object. You can add a listener to the object by using the
addActionListener()
method that requires a listener object as a parameter. The Listener
object must implement the ActionListener
interface that requires an
actionPerformed()
method to be written.
Checkbox
objects generates an ItemEvent
object. You can add
a listener to the object using the addItemListener()
method that requires a listener
object as a parameter. The listener must implement the ItemListener
interface
and is therefore required to write an itemStateChanged()
method.
A CheckboxGroup
object is really a group of Checkbox
objects and does not generate any events as a group, rather the individual objects generate the events as
for Checkbox
objects. If you need to find out the individual Checkbox
object that was chosen use the getSelectedCheckbox()
method.
Choice
objects generates an ItemEvent
object. You can add
a listener to the object using the addItemListener()
method that requires a listener
object as a parameter. The listener must implement the ItemListener
interface
and is therefore required to write an itemStateChanged()
method.
Label
objects do not generate events.
TextField
objects generate KeyEvent
objects when
a key is pressed in the box. This KeyEvent
object can be a key pressed, a key released
or a key typed. You can use the addKeyListener()
to add a listener to the
TextField
object. This method requires an object that has implemented the
KeyListener
interface that requires the object to implement the
keyPressed()
,keyReleased()
and
keyTyped()
methods. A TextField
object can also generate an
ActionEvent
object when the enter key is pressed in the text area. This
ActionEvent
has the same format as for the Button
component above.
List
objects generate an ItemEvent
object
when items are selected or deslected in the list. You can add
a listener to the object using the addItemListener()
method that requires a listener
object as a parameter. The listener must implement the ItemListener
interface
and is therefore required to write an itemStateChanged()
method. The List
object also generates an ActionEvent
object if the mouse is double-clicked or enter is
pressed on an item in the list.
TextArea
objects do not generate events.
Scrollbar
objects generate an AdjustmentEvent
object
when the Scrollbar
's bubble is moved. You can add a listener to the Scrollbar
object by using the addAdjustmentListener()
method that requires an object that
implements the AdjustmentListener
interface requiring the
adjustmentValueChanged()
method to be written.
The Component
class can generate many event types. The Component
is
the parent of all visible AWT components, providing the template for all AWT components. It can have:
A FocusEvent
which notifies a FocusListener
listener when the component has been selected.
A ComponentEvent
which notifies a ComponentListener
listener when the component has been resized, moved or made visible/invisible.
A HierarchyEvent
which notifies a HierarchyListener
when the hierarchy that this component belongs to changes.
A component can be added to a container's hierarchy using the add()
method
as we have used for myPanel.add(new Label("test"));
where myPanel
is an
object of the Panel
class, thus is a Container
object.
An InputMethodEvent
which notifies a ComponentListener
listener when the component has been resized, moved or made visible/invisible.
A MouseEvent
which notifies a MouseListener
listener that the mouse has entered or exited the component, clicked or pressed/released the mouse button
on the Component
object.
A MouseMotionEvent
which notifies a MouseMotionListener
listener when the mouse has been dragged or moved in the Component
object area.
A MouseWheelEvent
which notifies a MouseWheelListener
listener when the mouse wheel is rotated within the Component
object area. The
listeners mouseWheelMoved()
method is called when the mouse wheel event occurs.
The TextComponent
class can generate TextEvent
objects. It has the
Component
class as its parent and so can generate the same events as above for
Component
. A TextEvent
is generated when the object's text is
changed.
© 2006
Dr. Derek Molloy
(DCU).