Java AWT Components: Buttons, Lists, Text Fields, and More
Buttons
Buttons can be created with objects using the operator new:
Button button; button = new Button("Button");
Used in CADNA creation, the button will appear when displayed on the screen. This is also what CADNA will return to use when identifying the button event.
Button Events
One-touch button events can be captured by overloading the method action():
public boolean action(Event evt, Object obj) {
if (evt.target instanceof Button)
System.out.println((String) obj);
else System.out.println("Event No-Button");
}
The distinction between all existing buttons can be made by comparing the target object of the Event object with the button objects that we have set in our interface:
import java.net.*; import java.applet.Applet;
public class Button extends Applet {
Button b1, b2, b3;
public void init() {
b1 = new Button("Button B1");
b2 = new Button("Button B2");
b3 = new Button("Button B3");
this.add(b1); this.add(b2); this.add(b3);
}
public boolean action(Event evt, Object obj) {
if (evt.target.equals(b1))
System.out.println("Button B1 was pressed");
if (evt.target.equals(b2))
System.out.println("Button B2 was pressed");
if (evt.target.equals(b3))
System.out.println("Button B3 was pressed");
return true;
}
}
Choice Buttons (Dropdown Lists)
Choice buttons allow quick access to elements in a list. For example, you could implement colors and maintain the first selection in a choice button:
import java.net.*; import java.applet.Applet;
public class BotonSeleccion extends Applet {
Choice Selector;
public void init() {
Selector = new Choice(); Selector.addItem("Red");
Selector.addItem("Green"); Selector.addItem("Blue"); add(Selector);
}
public boolean action(Event evt, Object obj) {
if (evt.target instanceof Choice)
{String color = (String) obj; System.out.println("The chosen color is " + color);}
return true;
}
}
CADNA provides the addItem() method. The argument object of a choice event is returned. In the selection button handler, check in the first place if the event is effectively generated by a choice type button.
Checkbox Buttons
Checkbox buttons are frequently used as state buttons. They provide a yes or no (true or false) option. The state is returned in the object argument of the checkbox events; the argument is of boolean type: true if the box is selected and false otherwise.
Notice how the state is returned in the event argument, although it can be obtained through the methods getLabel() and getState() of the checkbox object.
import java.net.*; import java.applet.Applet;
public class Checkbox extends Applet {
Checkbox Fill;
public void init() {
Fill = new Checkbox("Fill");
add(Fill);
}
public boolean action(Event evt, Object obj) {
if (evt.target instanceof Checkbox)
System.out.println("CheckBox: " + evt.arg.toString());
return true;
}
}
Radio Buttons
Radio buttons can form a group within an interface using CheckboxGroup, which are groupings of checkbox buttons where there is always a single active button.
import java.net.*; import java.applet.Applet;
public class BotonRadio extends Applet {
CheckboxGroup Radio;
public void init() {
Radio = new CheckboxGroup();
add(new Checkbox("First", Radio, true));
add(new Checkbox("Second", Radio, false));
add(new Checkbox("Third", Radio, false));
}
}
Self-Contained Buttons
In this type of button, the site builds the event handler within the class itself, extended from Button. These buttons can be added to the application without having to worry about the events they could generate.
import java.net.*; import java.applet.Applet;
class OKButton extends Button {
public OKButton() {
setLabel("OK");
}
public boolean action(Event evt, Object obj) {
System.out.println("OK Button");
return true;
}
}
public class BotonAuto extends Applet {
OKButton button;
public void init() {
OKButton button = new OKButton(); add(button);
}
}
Note that there is no action() method in the Applet class BotonAuto, as the OKButton class will handle its events. If other objects were placed in the applet, an action() method might have been used to treat those objects’ events.
Labels
Labels provide a way to place static text on a panel, to display information that is normally unchanged by the user.
import java.net.*; import java.applet.Applet;
public class Label extends Applet {
public void init() {
Label Etiq1 = new Label("Hello Java");
Label Etiq2 = new Label("Another label"); setLayout(new FlowLayout(FlowLayout.CENTER, 10,10)); add(Etiq1); add(Etiq2);
}
}
Lists
Lists appear in user interfaces to facilitate the handling of many elements by operators. They are similar to choice buttons. The list is visible all the time, using a scroll bar to display elements that are not visible.
import java.net.*; import java.applet.Applet;
public class List extends Applet {
public void init() {
List l = new List(4, false);
l.addItem("Mercury"); l.addItem("Venus");
l.addItem("Earth"); add(l);
}
public boolean action(Event evt, Object obj) {
if (evt.target instanceof List)
System.out.println("List input: " + obj);
return true;
}
}
To access the selected elements, the methods getSelectedItem() or getSelectedItems() are used. For single-selection lists, either a double-click or a selection triggers the list’s action() method in the same way as with selection events in menus.
import java.net.*; import java.applet.Applet;
public class ListaMult extends Applet {
List lm = new List(6, true);
public void init() {
Button button = new Button("Accept");
lm.addItem("Mercury"); lm.addItem("Venus"); lm.addItem("Earth"); add(lm); add(button);
}
public boolean action(Event evt, Object obj) {
if (evt.target instanceof Button)
if ("Accept".equals(obj))
{String Selection[] = lm.getSelectedItems();
for (int i = 0; i < Selection.length; i++)
System.out.println(Selection[i]);
}
return true;
}
}
In this case of multiple selections in a list, an external event is used to trigger the actions associated with the list. In the example, we included a button that, when generating an event, will collect the selected elements in the list.
Text Field
Text fields can be created with empty spaces of a determined length, with predefined text, and with predefined text and a determined length.
import java.net.*; import java.applet.Applet;
public class TextField extends Applet {
TextField tf1, tf2, tf3, tf4;
public void init() {
// Empty text field
tf1 = new TextField();
// Empty text field with 20 columns
tf2 = new TextField(20);
// Predefined text
tf3 = new TextField("Hello");
// Predefined text in 30 columns
tf4 = new TextField("Hello", 30);
add(tf1); add(tf2); add(tf3); add(tf4);
}
public boolean action(Event evt, Object obj) {
if (evt.target instanceof TextField)
if (evt.target.equals(tf1)) System.out.println("Text Field 1: " + evt.arg.toString());
if (evt.target.equals(tf2)) System.out.println("Text Field 2: " + evt.arg.toString());
if (evt.target.equals(tf3)) System.out.println("Text Field 3: " + evt.arg.toString());
if (evt.target.equals(tf4)) System.out.println("Text Field 4: " + evt.arg.toString());
return true;
}
}
When the user enters text in a text field, a TextField event is generated, which can be captured with the action() method, as with other AWT components.
Text Areas
TextArea objects are used to occupy text elements of more than one line. The presentation can be both editable text and read-only text. You can allow the user to edit the text using the setEditable() method in the same way as in TextField.
import java.net.*; import java.applet.Applet;
public class AreaTexto extends Applet {
TextArea t1, t2;
public void init() {
Button btn = new Button("OK");
t1 = new TextArea(); t2 = new TextArea("Java Tutorial", 5, 40); t2.setEditable(false); add(t1); add(t2); add(btn);
}
public boolean action(Event evt, Object obj) {
if (evt.target instanceof Button)
if ("OK".equals(obj))
{String text = t1.getText();
System.out.println(text);
}
return true;
}
}
To access the current text in a text area, the getText() method is used. Text areas do not generate events by themselves, so external events must be used to know when to access the information contained in the TextArea. In this case, we used a button that, when pressed, will generate an event that will collect the text contained in the text area of the applet.