J.E.D.I.
7 Abstract Windowing Toolkit and Swing
7.1 Objectives
Without learning about graphical user interface GUI APIs, you would still be able to create quite a descent range of different programs. However, your applications are very
likely to be bland and unappealing to the users. Having a good GUI affects the usage of your application. This results in ease of use and enjoyment of use for the users of your
program. Java provides tools like Abstract Windowing Toolkit AWT and Swing to develop interactive GUI applications.
After completing this lesson, you should be able to: 1. Explain similarities and differences between AWT and Swing
2. Differentiate between components and containers 3. Design GUI applications using AWT
4. Design GUI applications using Swing 5. Describe how flow layout, border layout and grid layout position GUI components
6. Create complex layouts in designing GUI appllications
7.2 Abstract Windowing Toolkit AWT vs. Swing
The Java Foundation Classes JFCs, which is an important part of the Java SDK, refers to a collection of APIs that simplifies the development Java GUI applications. It primarily
consists of five APIs including AWT and Swing. The three other APIs are Java2D, Accessibility, and Drag and Drop. All these APIs assist developers in designing and
implementing visually-enhanced applications.
Both AWT and Swing provides GUI components that can be used in creating Java applications and applets. You will learn about applets in a latter section. Unlike some
AWT components that use native code, Swing is written entirely using the Java programming language. As a result, Swing provides a platform-independent
implementation ensuring that applications deployed across different platforms have the same appearance. AWT, however, does ensure that the look and feel of an application
run on two different machines be comparable. The Swing API is built around a number of APIs that implement various parts of the AWT. As a result, AWT components can still be
used with Swing components.
7.3 AWT GUI Components
7.3.1 Fundamental Window Classes
In developing GUI applications, the GUI components such as buttons or text fields are placed in containers. These are the list of important container classes provided in the
AWT.
Introduction to Programming II Page 88
J.E.D.I.
AWT Class Description
Component An abstract class for objects that can be displayed on the console and
interact with the user. The root of all other AWT classes. Container
An abstract subclass of the Component class. A component that can contain other components.
Panel Extends the Container class. A frame or window without the titlebar, the
menubar nor the border. Superclass of the Applet class. Window
Also extends Container class. A top-level window, which means that it cannot be contained in any other object. Has no borders and no menubar.
Frame Extends the Window class. A window with a title, menubar, border, and
resizing corners. Has four constructors, two of which have the following signatures:
Frame FrameString title
Table 15: AWT Container classes
To set the size of the window, the overloaded setSize method is used. void setSizeint width, int height
Resizes this component to the width and height provided as parameters.
void setSizeDimension d Resizes this component to d.width and d.height based on the Dimension d specified.
A window by default is not visible unless you set its visibility to true. Here is the syntax for the setVisible method.
void setVisibleboolean b
In designing GUI applications, Frame objects are usually used. Heres an example of how to create such an application.
import java.awt.; public class SampleFrame extends Frame {
public static void mainString args[] { SampleFrame sf = new SampleFrame;
sf.setSize100, 100;
Try removing this line sf.setVisibletrue;
Try removing this line }
} Here is the expected output for running SampleFrame:
Figure 7.1: Running SampleFrame
Note that the close button of the frame doesnt work yet because no event handling
Introduction to Programming II Page 89
J.E.D.I.
mechanism has been added to the program yet. Youll learn about event handling in the next module.
7.3.2 Graphics