Buttons

The Button component provides one of the most frequently used objects in graphical applications. When the user selects a button, it signals the program that something needs to be done by sending an action event. The program responds in its handleEvent() method (for Java 1.0) or its actionPerformed() method (defined by Java 1.1's ActionListener interface). Next to Label, which does nothing, Button is the simplest component to understand. Because it is so simple, we will use a lot of buttons in our examples for the next few chapters.

Button Methods

Constructors Button Labels Action Commands

With Java 1.1, every button can have two names. One is what the user sees (the button's label); the other is what the developer sees and is called the button's action command. Distinguishing between the label and the action command is a major help to internationalization. The label can be localized for the user's environment. However, this means that labels can vary at run-time and are therefore useless for comparisons within the program. For example, you can't test whether the user pushed the Yes button if that button might read Oui or Ja, depending on some run-time environment setting. To give the developer something reliable for comparisons, Java 1.1 introduces the action command. The action command for our button might be Yes, regardless of the button's actual label.

's label. Java 1.0 code, which only relies on the label, will continue to work. Furthermore, you can continue to write in the Java 1.0 style as long as you're sure that your program will never have to account for other languages. These days, that's a bad bet. Even if you aren't implementing multiple locales now, get in the habit of testing a button's action command rather than its label; you will have less work to do when internationalization does become an issue.

Miscellaneous methods
java.awt.Button[77,5,91x21,label=ZapfDingbats] 

Button Events

With the 1.0 event model, Button components generate an ACTION_EVENT when the user selects the button.

With the version 1.1 event model, you register an ActionListener with the method addActionListener(). When the user selects the Button, the method ActionListener.actionPerformed() is called through the protected Button.processActionEvent() method. Key, mouse, and focus listeners are registered through the Component methods of addKeyListener(), addMouseListener(), or addMouseMotionListener(), and addFocusListener(), respectively. Action

Example 5.1: Button Event Handling

import java.awt.*; import java.applet.*; class TheButton extends Button {
 TheButton (String s) {
 super (s);
}
public boolean action (Event e, Object o) {
 if ("One".equals(o)) {
 System.out.println ("Do something for One");
}
else if ("Two".equals(o)) {
 System.out.println ("Ignore Two");
}
else if ("Three".equals(o)) {
 System.out.println ("Reverse Three");
}
else if ("Four".equals(o)) {
 System.out.println ("Four is the one");
}
else {
 return false;
}
return true;
}
}
public class ButtonTest extends Applet {
 public void init () {
 add (new TheButton ("One")); add (new TheButton ("Two")); add (new TheButton ("Three")); add (new TheButton ("Four"));
}
} 

Figure 5.3: The ButtonTest applet

[Graphic: Figure 5-3]Keyboard

Buttons are able to capture keyboard-related events once the button has the input focus. In order to give a Button the input focus without triggering the action event, call requestFocus(). The button also gets the focus if the user selects it and drags the mouse off of it without releasing the mouse.

Listeners and 1.1 event handling

With the 1.1 event model, you register listeners, which are told when the event happens.

// Java 1.1 only import java.awt.*; import java.applet.*; import java.awt.event.*;
public class ButtonTest11 extends Applet implements ActionListener {
 Button b;
public void init () {
 add (b = new Button ("One")); b.addActionListener (this); add (b = new Button ("Two")); b.addActionListener (this); add (b = new Button ("Three")); b.addActionListener (this); add (b = new Button ("Four")); b.addActionListener (this);
}
public void actionPerformed (ActionEvent e) {
 String s = e.getActionCommand(); if ("One".equals(s)) {
 System.out.println ("Do something for One");
}
else if ("Two".equals(s)) {
 System.out.println ("Ignore Two");
}
else if ("Three".equals(s)) {
 System.out.println ("Reverse Three");
}
else if ("Four".equals(s)) {
 System.out.println ("Four is the one");
}
}
}