Java AWT

Introduction

Java provides a large number of built-in classes, which help us in designing graphical user interface (GUI). Most of these classes belong to the package java.awt and are collectively known as Abstract Windows Toolkit (AWT).

We are familiar with software like MS-WORD, MS-EXCEL etc. All of these software have a GUI i.e. when we run these software, we see graphical user interface.

We access various features of this software by selecting different options through keyboard or mouse. Lot of programming is needed to display the user interface.

Three important parts of AWT, which help in designing graphical user interfaces, are

  • Components
  • Containers
  • Layout Managers

All of the above play an important role in designing the GUI.

Components:

Components are Java’s building blocks for creating GUI’s. A component is an object having graphical representation that can be displayed on the screen and that can interact with the user. Examples of components are the buttons, check boxes and scrollbars of a typical graphical user interface. The java.awt.Component class is the abstract super class of the non-menu related AWT components. Class component can also be extended directly to create lightweight component. A lightweight is a component that is not associated with a native opaque window.

Some components types, such as buttons and scrollbars, are used directly for GUI control. Other kinds of components (those that inherit from the Container class) provide special organization. While the components like TextField, TextArea, List etc, are used to accept input from the user. The label is a different type of component whose purpose is to simply display the appropriate label to identify other components.

Java’s components are implemented by the many subclasses of the java.awt.Component and java.awt.Menu component super classes.

One way to organize this fairly large number of classes is to divide them into categories:

  • Visual components
  • Container components
  • Menu components
Component hierarchy:

The following diagram shows AWT the component hierarchy.

Java AWT
Containers:

The following diagram describes the container hierarchy

Java AWT

All the window–based applications start with a top-level window visible on the screen.

Similarly all GUI based java applications also start with a top-level window, which is referred to as top-level container in java’s terminology.

Containers are also java components that can contain other components. A component can be made visible only by adding it to a container or putting it inside a container.

All java GUI’s reside either in an applet or in a Frame. The applets and frames are the two top-level containers in java.

The applet is the top-level container for applets so the components must be added to it. Similarly the frame is the top-level component for the standalone applications.

For more complicated GUI’s it is convenient to divide the applets or frame into smaller regions. These regions might constitute, for example, a toolbar or a matrix of radio buttons. In java, GUI sub-regions are implemented most commonly with the panel container.

Panels, just like applets and frames can contain other components such as buttons, canvas, checkboxes, scrollbars, scrollpanes, text areas, textfields, and other panels.

Complicated GUI’s sometimes have very complicated containment hierarchies of panels with in panels within panels. And so on, down through many layers of containment.

As containers are also components so the methods available for components are also available for containers due to inheritance.

The most commonly used method for the containers is add() method using which the components are added the container its general form is.

void add (Component comp);

All the components are the sub-classes of the component class so any component can be added to the container using the add() method.

Copyright 1999- Ducat Creative, All rights reserved.