Introduction to the Abstract Windowing Toolkit (AWT)

Introduction to the AWT

The Java "Abstract Window Toolkit" (AWT) is both powerful and flexible. The AWT provides many classes for programmers to use. It is your connection between your application and the GUI components. It is a Java package that can be used in any Java program by importing "java.awt.*" using the "import" keyword.

The AWT hierarchy

AWT includes the following classes:

  • CardLayout
  • GridLayout
  • BorderLayout
  • FlowLayout
  • GridBagLayout
  • CheckboxGroup
  • component
  • List
  • Color
  • Label
  • Scrollbar
  • Dimension
  • Event
  • FontMetrics
  • FileDialog
  • Font
  • GridBagConstraints
  • Graphics
  • Insets
  • Image
  • Point
  • Rectangle
  • Polygon
  • Toolkit


AWT includes the following interfaces:

  • LayoutManager
  • MenuComponent
  • Components and Containers:

     There are two user-interface classes in the AWT to focus on: "Containers" and "Components".


     Containers handle events that occurr to the Components. Containers (Windows, Frames, Dialogs and Panels) can contain components, thus can be added to Containers.

    A quick summary of container methods:

    • getComponents()
    • remove(Component)
    • add(Component)
    • setLayout(LayoutManager)
    • getLayout()
    • add(String, Component)

    All of the methods in a Component can be used in a Container. Every Container is a Component since it is derived from a Component, thus it used and behaves like a Component.


     Components are Buttons, TextAreas, Scrollbars, etc. In other words the visible UI controls that the user interacts with, all of which have been added to a Container.

    A quick summary of the common methods is the following:

    • setBackground(Color)
    • getBackground()
    • setFont(Font)
    • getFont()
    • mouseDown(Event, int, int)
    • show()
    • update(Graphics)
    • move(int, int)
    • resize(int, int)
    • paint(Graphics)


     A Layout is used to set an attractive view to our frame or window. Since the LayoutManager class is abstract, we can't use it directly. We must make a sub class of that and override all its functions and attributes.

     There are various layouts, but the ones pre-defined are the following.

    BorderLayout: This scheme lays out the comonent in the following 5 ways:

    1. East: Eastern part of the Container
    2. West: Western part of the Container
    3. Center: Centered in the Container
    4. North: Northern part of the Container
    5. South: Southern part of the Container

    Other Layouts:

    • CardLayout:
    • GridLayout: Components in a grid-like fashion rather than "Center" or "North".
    • GridBagLayout: The style used in HTMLTable.
    • FlowLayout: Component to be laid in a flow (or row) and aligned (left, right, center).
    • DefaultLayout: No layout, the Container will not change position during an update.

    To use a layout we must call setLayout() for the Container with an instance of a LayoutManager. Use of each layout is different. For BorderLayout you specify the type of layout (North, South, etc.) by passing a string (i.e "North", "South", etc. to the method add() in a Container.

    For example:

    add("Center", myComponent);

    adds myComponent to the Container and centers it. For different layouts, the first String syntax is different.

    The following example displays a simple AWT program containing a tollbar and 3 buttons (cut, copy and paste buttons).

    import java.awt.*;
    import java.awt.event.*;
    public class ToolbarFrame23 extends Frame implements ActionListener {
    Button cutButton, copyButton, pasteButton;
    public ToolbarFrame23() {
    super("Toolbar Example (AWT)");
    setSize(450, 250);
    addWindowListener(new BasicWindowMonitor());
    Panel toolbar = new Panel();
    toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
    cutButton = new Button("Cut");
    copyButton = new Button("Copy");
    pasteButton = new Button("Paste");
    add(toolbar, BorderLayout.NORTH);
    public void actionPerformed(ActionEvent ae) {
    public static void main(String args[]) {
    ToolbarFrame1 tf1 = new ToolbarFrame1();

    The BasicWindowMonitor Class is also needed and is frequently used in other later examples:

    import java.awt.event.*;
    import java.awt.Window;
    public class BasicWindowMonitor extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
    Window w = e.getWindow();




    To use menus you need to create a MenuBar and Menus with MenuItems, then attach the MenuBar to your application via setMenuBar. You can even embed Menus in MenuItems.

    • MenuBar mb = new MenuBar();
    • Menu top = new Menu("File");
    • Menu sub = new Menu("New");
    • sub.add(new MenuItem("Document"));
    • sub.add(new MenuItem("Image"));
    • sub.add(new MenuItem("Message"));
    • top.add(sub);
    • top.add(new MenuItem("Save..));
    • top.add(new MenuItem("Open"));
    • setMenuBar(mb);

    Drawing Lines

    The following is an example of drawing lines:

    g.drawLine(x1, y1, width, height);

    Drawing Rectangles:

    To draw a rectangle you would use one of the following:

    • drawRect(x1, y1, width, height)
    • drawRect3D(x1, y1, width, height, raised)
    • drawRoundRect(x1, y1, width, height, arcWidth, arcHeight)

    Drawing images

    Drawing an image is done with drawImage, a method in the Graphics class. All Components implement the ImageObserver interface and applets can use getImage that retrives a gif/jpeg and returns an Image class, as in the following:

    Image img = getImage(getDocumentBase(), "mypicture.gif");

    public void paint(Graphics g)

    {  g.drawImage(img, 10, 20, this);