Search This Blog

Friday, 5 November 2021

Introduction to Abstract Window ToolKit (AWT) & Swing

0 comments

AWT & Swing

AWT is a package that contains numerous classes and methods that allow a programmer to create and manage windows applications using Java.  A collection of classes are provided in java.awt package for creating and running GUI (Graphical User Interface) applications in Java.  java.awt is one of the largest packages provided by Java.  AWT lays the foundation upon which Swing and Applets built.

Fig. Hierarchy of AWT and Swing Classes

AWT defines and manages a basic set of components that support a usable, but limited, graphical interface.  One reason for the limited nature of the AWT is that its components rely on platform-specific native windows, or peers.  Because of this they are referred to as heavy weight.

Two most common classes provided in AWT for creating windows applications are Panel and Frame.  Panel is a type of window used by applets, whereas Frame is another type of window used by standard window application.  Much of the functionality of window are defined in the base classes - Panel and Frame.

Swing Technology

Swing addresses the limitations associated with the AWT's components through the use of two key features:  lightweight components and a pluggable look and feel.  Swing components are lightweight.  This means that a component is written entirely in Java.  Lightweight components have two important advantages: efficiency and flexibility.  

For example, a lightweight component can be transparent, which enables non rectangular shapes.  Furthermore, because lightweight components do not translate into platform-specific peers, the look and feel of each component is determined by Swing.  This means that each component can work in a consistent manner across all platforms.

Swing's pluggable look-and-feel is made possible because Swing uses a modified version of a classic component architecture called Model-View-Controller (MVC).  In MVC terminology, the model corresponds to the state information associated with the component.  For example, in the case of a check box, the model contains a field that indicates if the box is checked or unchecked.

The view determines how the component is displayed on the screen, including any aspects of the view that are affected by the current state of the model.  The controller determines how the component reacts to the user.  For example, when the user clicks on a check box, the controller reacts by changing the model to reflect the user's choice (checked or unchecked).

Containers and Components of Swing

A Swing GUI consists of two types of  visual elements: Containers and Components.  A component is an independent visual control, such as a push button or a text box, where as a container is a special type of component that is designed to hold other components.  All Swing GUIs will have at least one container.

Because containers are components, a container can also hold other containers.  Swing defines two types of containers:  top-level containers and light-weight containers.  Top-level containers are used to create and display main windows, and light-weight containers can be used to create child windows (window with in another window).

JFrame, JApplet, JWindow and JDialog are four classes that define the top-level containers in Java.  A top-level container can't be contained within any other container.  The commonly used top-level container for stand-along Windows applications is JFrame.  The top-level container JApplet is used for creating the main window of Applets.

Light-weight containers are used for organizing and managing groups of related components collectively, because a light-weight container can be contained within another container.  Moreover, light-weight containers are derived from the base class JComponent, which is the base class for all component classes.  Examples of light-weight containers are JPanel and JRootPane.

Component Classes of Swing Package

All Swing components are derived from the parent class JComponent.  The only exception to this are the top-level containers - JFrame, JWindow, JDialog and JApplet.  JComponent provides the functionality that is common to all components of swing.  JComponent inherits the AWT classes Container and Component and hence a Swing component is built on and compatible with an AWT component.

All of Swing's components are represented by classes defined within the package javax.swing.  All component classes in swing package begins with the letter J.  The following are some of the component classes defined in this package: 

JLabel - defines a label

JButton - defines a push button

JScrollBar - defines a scroll bar

Swing programs differ from the console-based applications by providing Graphical User Interface (GUI) elements for user interaction.  Console-based applications make user of terminal (command-line) in which the input and output are handled in the form of text.  Whereas swing applications are graphical applications in which Graphical User Interface (GUI) elements are used for user interaction.

There are two types of Java programs in which Swing classes are typically used.  The first is a desktop application, and the second is the applet.  Desktop applications  are windows application that can run on a stand along computer.  On the other hand, applets are java applications that run on a browser.  They are used for developing client-side web application that usually runs on Internet.

Leave a Reply