Java Lesson 17: Abstract Window Toolkit (AWT) – Buttons and Other Controls

1
34

Introduction

As we learned in the previous lesson tutorial, Java provides the Abstract Window Toolkit (AWT) to facilitate development of applets and programs with Graphical User Interfaces (GUI) instead of text-only console I/O. The AWT includes dozens of classes and interfaces, and we’ll continue our discussion of several of the most common in this lesson. Applets and standalone Java windows programs use the AWT; we will use applets in this tutorial lesson because they make it easier to learn the concepts.

 

Handle Events with Buttons

Buttons are one of the most common controls provided by Java’s AWT package. The Button class provides the necessary methods to create a button that responds to being clicked by the user. Two steps are necessary: creating the button, and setting up the event handling. An event occurs each time the user clicks a button. You specify how to handle the event with the actionPerformed() method.
Our first example, Applet17a.java, creates two buttons and responds when one of the buttons is clicked.

The Button constructor takes one argument which is the name of the button. The name (e.g., OK, Cancel) is displayed on the button. Looking at our applet, Line 17 implements ActionListener which is responsible for listening to events. Lines 20 and 21 declare two buttons. In lines 28 and 29, a new button, named Button1, is created and added to the window. Line 30 completes the set up of Button1 by adding an action listener for the button. Button2 is set up in a similar way. We then override the actionPerformed() method. The method uses an if-else statement to determine which of the two buttons was clicked. The string variable msg is set appropriately depending upon which button was clicked. Finally, the applet repaints the window.

Below is a screenshot showing the compilation and execution of Applet17a.

Compile and execution of Applet17a.java

When the applet starts it looks like the screenshot below. The two buttons are visible in the window.

Execution of Applet17a. Two buttons are visible.

A message is displayed in the window when a button is clicked.

Execution of Applet17a, message displayed when Button 1 is clicked.

 

Enhance the Look-And-Feel with More Controls

In addition to buttons, you can use other controls in your programs. Controls allow you to create the Graphical User Interface (GUI) of modern application programs. These include labels, text fields, dialog boxes, scroll bars, radio buttons, and menus. Layout Managers and Fonts determine the look-and-feel.

Display Text In Labels

A label is a string displayed in a window. Labels only display information and cannot be clicked by the user. The Label constructor takes one argument which is the text to display in the label. Like a button, the label is declared and added to the window. Applet17b.java displays two labels in the window.

The compilation and execution of Applet17b is shown below.

Compile and execution of Applet17b.java, which displays two labels.

Execution of Applet17b, which displays two labels.

Combining Buttons and Labels

Naturally, you can develop an applet window which has more than one type of control. Applet17c.java combines what we learned in the previous two examples. It creates two buttons and two labels. The corresponding label is updated with the number of times the button has been clicked.

The compilation and execution of Applet17c is shown below. The first button was clicked twice, and the second button was clicked eight times.

Compile and execution of Applet17c.java, which has two buttons and two labels which are updated every time a button is clicked.

Execution of Applet17c which has two buttons and two labels which are updated when the buttons are clicked.

Get User Input With TextField

A textfield is like a label in which the user can type one line of text (letters, numbers, and punctuation). There are several TextField constructions, but the one we will use takes one argument, which is an int specifying the number of characters in the textfield. The getText() method returns a String containing the text the user entered. Applet17d.java creates a textfield, a button, and a label. When the user enters text into the textfield and clicks the button, then the label is updated with the contents of the textfield.

The compilation and execution of Applet17d is shown below. The characters “Java is fun!” is entered into the textfield and then the button is clicked.

Compilation and execution of Applet17d.java, which has a textfield, a button, and a label.

Execution of Applet17d, before characters are entered into the textfield.

Execution of Applet17d, after characters are entered into the textfield and the button is clicked.

Customize the Window With Layout Manager and Font

Layout Managers arrange the controls (buttons, labels, text fields, etc.) in a certain order. There are several layout managers. We have been using the default FlowLayout which arranges the controls left-to-right and top-to-bottom. Another layout is GridLayout, which arranges the controls on a two-dimensional grid with rows and columns. This is ideal for displaying a checkerboard or a photo gallery. The number of rows and columns is set with the constructor. In Applet17e.java, the number of rows is three and the number of columns is one; this makes the controls stack in a vertical column instead of being positioned left to right as in the previous examples. The font is set to Courier italic bold. We also use the label’s setForegound() method to change its text color to blue.

The compilation and execution of Applet17e is shown below. The characters “Java is fun!” is entered into the textfield and then the Echo Msg button is clicked.

Compile and execution of Applet17e.java, which demonstrates the use of a Layout Manager, Fonts, and text Colors.

Execution of Applet17e, which uses the GridLayout manager to display three controls.

 

Summary

This lesson continued exploring Java’s Abstract Window Toolkit (AWT) by demonstrating Buttons, TextFields, Fonts, and Layout Managers in Java applets.

Next Lesson

Another way to develop GUI-based Java programs is with Java Swing, which is the subject of our next tutorial lesson.

 

1 COMMENT

  1. […] Getting input from the user is just as important as displaying output. The next tutorial lesson will examine handling events from AWT buttons and other controls. […]

Leave a Reply