Introduction of AWT

  • The Abstract Window Toolkit (AWT) is Java's original platform-independent windowing, graphics, and user-interface widget toolkit.

  • The AWT is both powerful and flexible. Newcomers, however, often find that its power is veiled. The class and method descriptions found in the distributed documentation provide little guidance for the new programmer

  • The AWT is now part of the Java Foundation Classes (JFC) - the standard API for providing a graphical user interface (GUI) for a Java program.

  • Building a GUI involves creating "abstract" components such as buttons and windows, which are then mapped to "concrete" components for a specific platform.

  • AWT is also the GUI toolkit for a number of Java ME profiles.
  • Example - Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT.

Creating a Graphical User Interface

  • GUI programming in Java is based on three concepts:

    • Components- A component is an object that the user can see on the screen and-in most cases-interact with.
    • Containers- A container is a component that can hold other components.
    • Events- An event is an action triggered by the user, such as a key press or mouse click. Designing a graphical user interface involves creating components, putting them into containers, and arranging for the program to respond to events.

  • Components are objects, so they're created by invoking a constructor.

  • A button would be created by using a constructor belonging to the Button class.

  • The most commonly used constructor has one argument (the button's label):

  •  Button b = new Button("Testing"); 
  • For a component to be visible, it must be added to a container (typically a frame) by the add method.

  • To detect when an event occurs, a special " Listener " object can be attached to a component.

  • When the user performs an action that involves the component, a method belonging to the listener object will be called automatically.

AWT Class Hierarchy

AWT Class Hierarchy


  • Java Applets: import java.applet.Applet;
    • embedded in another application (not run on its own)
    • must be superclass of applet embedded in a Web page
    • subclass of Panel (which was a subclass of Container)

  • Container: import java.awt.*;

    • component that can contain other AWT components
  • Component:import java.awt.*;
    • interacts with the user, has graphical representation, displayed on the screen
    • abstract superclass of the nonmenu-related AWT components
    • Examples: buttons, checkboxes, text fields
    • Has methods for sizing and moving
    • public void paint(Graphics g) // Paints this component - don't call directly
    • void repaint(); // Requests this component be repainted

  • Graphics:import java.awt.*;
    • abstract base class for all graphics contexts
    • allow an application to draw onto components
    • encapsulates state information needed for the basic rendering color, font, object on which to draw, etc.
    • public void drawLine(int x1, int y1, int x2, int y2);
    • void drawString(String str, int x, int y);
    • void drawOval(int x, int y, int width, int height); // x,y: upper-left
    • void setColor(Color c); // set current drawing color
    • void setFont(Font font);

  • TextField:import java.awt.*;
    • component that allows for the editing of a single line of text
    • String getText(); // returns current text in the text field
    • void setText(String t); // set text for the text field

  • List:import java.awt.*;
    • List of String: General "container" but NOT an AWT Container
    • void add(String item) - adds an item to the end of the scrolling list
    • int getSelectedIndex() - gets the index of the selected item on the list
    • void remove(int position) - removes the item at the specified position index

  • ActionListener:import java.awt.event.*;
    • interface for receiving action events - used by Buttons, Timers
    • void actionPerformed(ActionEvent e); // must implement this
    • public class SimulationApplet extends java.applet.Applet
    • implements java.awt.event.ActionListener

  • ActionEvent:import java.awt.event.*;
    • event which indicates that a component-defined action occurred
    • Object getSource(); // Returns reference to initiating Object
    • String getActionCommand(); // returns the command string