Processing ActionEvents
The class ActionEvent
provides three member variables to process received messages: type
, command
and source
.
-
int type
: This variable contains the type of the ActionEvent
as an int
. Table 1 lists the possible values:
Value | Description |
BUTTON_PRESSED | The message has been triggered by a Button . |
ITEM_SELECTED | An item of a List or ComboBox has been selected. |
VALUE_CHANGED | A numerical value has been changed (e.g. of a Slider ). |
STATE_CHANGED | The state of a RadioButtons or CheckBox has been changed. |
Table 1: Possible values of member variable jcontrol.ui.wombat.event.ActionEvent.type
-
String command
: Using the ActionCommand
, GUI components are able to add String information to an ActionEvent
. The receiver can access this information via the member variable command
. The kind of information depends on the component. Button
s, for example, will transmit their label text. Note that the command
string may also be null
.
-
ActionProducer source
: The member variable source
contains a reference to the object which issued the ActionEvent
.
In the following excerpt of the example program are two techniques demonstrated of how to determine the source of an ActionEvents
. At first, the method onActionEvent
checks if the received event has been triggered by a Button
(event.type
). Thereafter will be decided if the left or right Button
has been pressed. The left Button
is hereby identified by comparing event.command
with the Button
's label. The right Button
is identified by using event.source
. The last method may seem more elegant, but assumes the presence of a reference to the Button
as a variable of the class instance. This may not be the case, if the ActionListener
is implemented by a separate class.
| ... |
| 53 public void onActionEvent(ActionEvent event) { |
| 54 // check whether this is a BUTTON_PRESSED event |
| 55 if (event.type == ActionEvent.BUTTON_PRESSED) { |
| 56 |
| 57 // recognize event's source by using getActionCommand() |
| 58 if ( "Left Button".equals(event.command)) |
| 59 label.setText("You pressed the left button!"); |
| 60 |
| 61 // recognize event's source by using getSource() |
| 62 if (event.source == button_right) |
| 63 label.setText("The right button was hit!"); |
| 64 } |
| 65 } |
| ... |
The kinds of ActionEvent
s that are generated by the components of JControl/Wombat are described in the according API documentation. However, all components share the presence of the method setActionListener(ActionListener listener)
. That way, an application program may individually process the user triggered events and define, which events it will react to and which events will be ignored.