AWT Components

A component is an object with a graphical representation that can be displayed on the screen and that can interact with the user. The Component class is the abstract parent of the nonmenu-related AWT components.

Button (java.awt.Button)

To create a Button object, simply create an instance of the Button class by calling one of the constructors. The most commonly used constructor of the Button class takes a String argument, that gives the Button object a text title. The two constructors are:

  Button()              // Constructs a Button with no label. 
  Button(String label)  // Constructs a Button with the specified label. 

Figure 9.2. A Button Component

A Button Component

When a user presses on a Button object an event is generated. The label of the button that is pressed can be obtained.

Checkboxes (java.awt.Checkbox)

Checkboxes ave two states, on and off. The state of the button is returned as the Object argument, when a Checkbox event occurs. To find out the state of a checkbox object we can use getState() that returns a true or false value. We can also get the label of the checkbox using getLabel() that returns a String object.

Figure 9.3. A Checkbox Component

A Checkbox Component

Radio Buttons (java.awt.CheckboxGroup)

Is a group of checkboxes, where only one of the items in the group can be selected at any one time.

Figure 9.4. A Radio Button Component

A Radio Button Component

Choice Buttons (java.awt.Choice)

Like a radio button, where we make a selection, however it requires less space and allows us to add items to the menu dynamically using the addItem() method.

Figure 9.5. A Choice Button Component

A Choice Button Component

Labels (java.awt.Label)

Allow us to add a text description to a point on the applet or application.

Figure 9.6. A Label Component

A Label Component

TextFields (java.awt.TextField)

Are areas where the user can enter text. They are useful for displaying and receiving text messages. We can make this textfield read-only or editable. We can use the setEditable(false) to set a textfield read-only. There are numerous ways that we can construct a Textfield object:


 TextField text1 = new TextField();                // no properties
 TextField text2 = new TextField("Some text");     // a textfield with a 
                                                   // predefined String
 TextField text3 = new TextField(40);              // a textfield with a 
                                                   // predefined size
 TextField text4 = new TextField("Some text", 50); // combination of the two
 

Figure 9.7. A TextField Component

A TextField Component

An Example Component Application

To show these components in action we can write a short piece of code that displays these components, but they do not have any events behind them - they work visually but do not have any true program function. Figure 9.8, “A Component Applet” shows an example applet that details all the previous components.

Figure 9.8. A Component Applet

A Component Applet

You can see this applet running here - ComponentApplet.html and the code segment is here - ComponentApplet.java

 1 
 2 
 3 // The Component Applet that displays several components
 4 
 5 import java.applet.Applet;
 6 import java.awt.*;
 7 
 8 public class ComponentApplet extends Applet
 9 {
10 	public void init()
11 	{
12 	  Button b = new Button("Test Button");
13 	  this.add(b);
14 
15 	  Checkbox cb = new Checkbox("Test Checkbox");
16 	  this.add(cb);
17 
18 	  CheckboxGroup cbg = new CheckboxGroup();
19 	  this.add(new Checkbox("CB Item 1", cbg, false));
20 	  this.add(new Checkbox("CB Item 2", cbg, false));
21 	  this.add(new Checkbox("CB Item 3", cbg, true));
22 	
23 	  Choice choice = new Choice();
24 	  choice.addItem("Choice Item 1");
25 	  choice.addItem("Choice Item 2");
26 	  choice.addItem("Choice Item 3");
27 	  this.add(choice);
28 
29 	  Label l = new Label("Test Label");
30 	  this.add(l);
31 
32 	  TextField t = new TextField("Test TextField",30);
33 	  this.add(t);
34 	}
35 }
36  
37