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.
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.
When a user presses on a 
Button object an event is generated. The label of the
button that is pressed can be obtained.
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.
Is a group of checkboxes, where only one of the items in the group can be selected at any one time.
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.
Allow us to add a text description to a point on the applet or application.
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
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.
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 © 2006
Dr. Derek Molloy
(DCU).