Professional Documents
Culture Documents
Building GUIs With AWT
Building GUIs With AWT
The graphical user interface (refer to GUI henceforth) is an important part of the modern day application. Like
most programming libraries, the JDK allows you to develop GUI front ends by using the Abstract Windowing
Toolkit or more commonly know as the AWT.
The basic GUI building blocks in AWT are Components and Containers.
Component Class
Component is the base class of all AWT widgets (eg. Button, List, Choice, etc.). We will use the word component
to refer to AWT widgets collectively and Component to refer specifically to the class. As the base class,
Component provides the following methods which are common across all components:
The above is not an exhaustive list of all methods; please consult the Component API documentation.
Components of Swing
JLabel
JLabel is a display area for a short text string or an image, or both. A label does not react to input events.
JButton
JButton is a lightweight button.
JTextField
JTextField is a lightweight component that allows the editing of a single line of text.
JTextArea
A JTextArea is a multi-line area that displays plain text.
Container Class
The Container class, as the name implies acts as a container for components. To built a GUI, you add
components in to Containers. Here is the catch: since Containers are themselves subsclass of Component, you
can add Containers to Containers! Using this technique, we can build very complex GUIs.
Lets take a bird's eyeview of building a simple GUI with Components and Containers before we proceed further.
1 import java.lang.*;
2 import java.awt.*;
3 public class AFrame {
4 public AFrame() {
5 Button button = new Button("Press Me!");
6 button.setBackground(Color.red);
7 button.setForeground(Color.white);
8 Frame frame = new Frame("A Frame");
9 frame.setLayout(new FlowLayout());
10 frame.setBackground(Color.yellow);
11 frame.add(button);
12 frame.setSize(200, 100);
13 frame.setVisible(true);
14 }
15 public static void main(String[] args) {
16 AFrame aFrame = new AFrame();
17 }
18 }
line 2, Unlike java.lang package which is imported by default, you have to import java.awt package manually
otherwise your program will not compile.
line 5, Recall Button is a subclass of Component. We create an instance of Button.
line 6 and 7, Sets button's foreground and background color.
line 8, We create an instance of Frame which is a subclass of Container.
line 9, Set the layout or arrangement viz. how components in frame will be arranged.
line 11, Add the button to the frame for display.
line 12, Sets the frame's size for display. The width and height is 200 by 100 pixel respectively.
line 13, Finally, when all the setup is completed, we display the frame by setting its visible property to true.
Containers of Swing
JFrame
JFrame is a lightweight top-level window with a title and a border.
Sample 1: JFrameExample
Source Code:
//JframeExample.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JDialog
JDialog is a lightweight top-level window with a title and a border that is typically used to take some
form of input from the user. A dialog can be either modeless (the default) or modal. A modal dialog is
one which blocks input to all other toplevel windows in the application, except for any windows created
with the dialog as their owner
Sample 1: JDialogExample.java
Source Code:
// JDialogExample.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Sample 1: JPanelExample.java
Source Code:
// JPanelExample.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
The layout of components within a Container is done by LayoutManager. Before you add any components to a Container you
need to set the layout or arrangement of the Container using setLayout() method. The LayoutManager controls the placement
of components according to it’s the LayoutManager's layout policy. The LayoutManager controls all aspect of a component
in the Container: its placement, size, orientation, etc.
The JDK comes with 5 LayoutManagers. We will examine only the following 3: FlowLayout, GridLayout and BorderLayout.
FlowLayout
FlowLayout arranges components in a Container like sentences in a page; from left to right and from top to
bottom. FlowLayout will try to fit as many components on a line as possible before moving them to the next. When
you resize a Container, FlowLayout will rearranges the components according to the new Container's size.
1 import java.lang.*;
2 import java.awt.*;
3 public class FlowLayoutEx {
4 public FlowLayoutEx() {
5 Frame frame = new Frame("FlowLayout");
6 frame.setLayout(new FlowLayout());
7 frame.add(new Button("One"));
8 frame.add(new Button("Two"));
9 frame.add(new Button("Three"));
10 frame.setSize(200, 100);
11 frame.setVisible(true);
12 }
13 public static void main(String[] args) {
14 FlowLayoutEx ex = new FlowLayoutEx();
15 }
16 }
Notice that in line 6, we set the FlowLayout to be the layout of the frame. All subsequent add ( line 7 to 9) will be
under FlowLayout's control. When you run FlowLayoutEx program, you will see the frame as shown in figure.
When the frame is resized, the buttons will adjust to accommodate the new size.
GridLayout
The GridLayout devides the Container into a specified number of rows and columns, very much like a matrix.
Components in the grid are arranged from left to right and from top to bottom. The size of each cell is the size of
the largest component in the grid.
BorderLayout divides a container into 5 region. They are “north", “south", “east", “west" and “center". When you
add a component to a BorderLayout managed container, you need to specify the region or your component will
not show up when you display the container.
1 import java.lang.*;
2 import java.awt.*;
3 public class BorderLayoutEx {
4 public BorderLayoutEx() {
5 Frame frame = new Frame("BorderLayout");
6 frame.setLayout(new BorderLayout());
7 frame.add(new Button("North"), BorderLayout.NORTH);
8 frame.add(new Button("South"), BorderLayout.SOUTH);
9 frame.add(new Button("East"), BorderLayout.EAST);
10 frame.add(new Button("West"), BorderLayout.WEST);
11 frame.add(new Button("Center"), BorderLayout.CENTER);
12 frame.setSize(200, 200);
13 frame.setVisible(true);
14 }
15 public static void main(String[] args) {
16 BorderLayoutEx ex = new BorderLayoutEx();
17 }
18 }
Note lines 7 to 11; we specify the region the component is to be added. The _rst parameter of add() is the
component itself, in this case a Button while the second is the position. The result of BorderLayoutEx is shown in
figure
SwingApplication
Source Code:
// SwingApplication.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
1. FlowLayout
Source Code:
// FlowLayoutTest.java
import java.awt.*;
import javax.swing.*;
2. GridLayout
Source Code:
// GridLayoutTest.java
import java.awt.*;
import javax.swing.*;
3. BorderLayout
Source Code:
// BorderLayoutTest.java
import java.awt.*;
import javax.swing.*;
Sample 1: ComponentsSample
Source Code:
// ComponentsSample1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Sample 2: ComponentsSample2
Source Code:
// ComponentsSample2.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
oCenterPanel.add( oRadioButton1 );
oCenterPanel.add( oRadioButton2 );
oCenterPanel.add( oComboBox );
oMenu.addSeparator();
oMenu.addSeparator();
setJMenuBar( oMenuBar );
Event-Delegation Model
In Java, events are objects. The java.awt.event. package defines a rich hierarchy of event types. Through this
package, instances of the various event classes are constructed when users are GUI components. It is up to the
programmer to decide how to handle the generated event. For example, when a user clicks a button, the system
constructs an instance of class ActionEvent, in which it stores details about the event (when, where, and so on).
At this point, the programmer has three options:
// ButtonDelegateTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
getContentPane().add( oButton );
addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent eWindow )
{
System.exit( 0 );
}
}
);
}