6.01 - Basic GUI Components

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 (Abstract Window Toolkit) 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 6.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 6.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 6.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 6.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 6.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 6.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 6.8, “A Component Application” shows an example application that details all the previous components.

Figure 6.8. A Component Application

You can see this application running in Figure 6.9. 

package ee402;

import java.awt.*;

public class ComponentApplication extends Frame {

        public ComponentApplication() {
                super("EE402 Component Application");
                this.setLayout(new FlowLayout());
                Button b = new Button("Test Button");
                Checkbox cb = new Checkbox("Test Checkbox");
                CheckboxGroup cbg = new CheckboxGroup();
                this.add(new Checkbox("CB Item 1", cbg, false));
                this.add(new Checkbox("CB Item 2", cbg, false));
                this.add(new Checkbox("CB Item 3", cbg, true));
                Choice choice = new Choice();
                choice.addItem("Choice Item 1");
                choice.addItem("Choice Item 2");
                choice.addItem("Choice Item 3");
                Label l = new Label("Test Label");
                TextField t = new TextField("Test TextField",30);

        public static void main(String[] args) {
                new ComponentApplication();

These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
Derek Molloy,
12 Nov 2013, 03:29