Responding to Controls
Except for labels, which are passive controls, all controls generate events when they are accessed by the user. For example, when the user clicks on a push button, an eve that identifies the push button. In general, simply implements the •appropriate interface and then registers an event listener for each control that you need to monitor. As explained in Chapter 20, once a listener has been installed, events are automatically sent to it ln the sections that follow, the appropriate interface for each .control is ,specified.
The easiest control to use a label A Allie is an object of type Label, and it contains a
string, which it displays. tables are passive controls that do not support any interaction with the user. "Label defines the following constructors:
Labe1(String str, int how)
The first version creates a blank label. The second version creates a label that contains the string specified This string is left-justified. The third version creates a label that contains the string specified by the alignment specified by how. The value of hoto must be one of these constants: Label.LEFT, Label.RIGHr, or Label.CENTER.
The most widely used control is the push button, A push button is a component that
contains a label and that generates an event when it is pressed. Push buttons arc objects
of type' Button. Button defines these two constructors:
The first version creates an empty button. The second creates a button that contains str
as a label. '
After a button has been created, you can set its label by calling set Label(). You can
retrieve its label by calling getf.abelt ), These methods are as follows: '
void set Label(String str)
String get Label( )
Here,becomes the' new label for the button.
Each time a button is pressed, an action event is generated. sent to any listeners .that previously registered an interest in receiving action event notifications from that component. Each listener implements the Action Listener interface. interface defines the action Periormed( ) method, which is called when an event occurs. An Adion Evcnt I object is supplied as the argument to this method. It contains both a reference to the button that generated the event and a reference to the string that is the label of the , button. Usually, either value may be used to identify the button, as you will sec. Here is an example that creates three buttons labeled "Yes;" "No," and "Undecided." Each time one is pressed, a message is displayed that reports which button has been.pressed. In this version, the label of the button is used to determine which button has been pressed: The label is obtained by calling the get Action .
Sample output from the .Button Demo program is shown As mentioned, in addition to comparing button labels, you can also determine . which button has been pressed, by comparing the object obtained from the .method to the button objects that you added to the window.. To do this, you must keep a list of the objects when they are' added ..The following applet shows this approach.
In this version, the programmer stores each button reference in an array when the buttons are added to the applet window. (Recall that the add( ) method returns a reference-to the button when it is added.) Inside action Performed( ), this array is then used to which button has been pressed. For simple applets, it is usually easier to recognize buttons by their labels. However, in situations in which you.will be changing the label inside a button during the execution of your program, or using buttons that have the same label, it may be easier to determine which button has been pushed by using its object reference.