My other sites

Latest News

New Summer Look :)

Applets AWT Components and Events

  Applets - Applications

What is AWT?

Why AWT?

The AWT Package


Basic Objects: Component

Basic Objects: Container

Component Hierarchy

First AWT Example

Event Handling

The Initial Event Model

An AWT Example Using The Initial Event Model

The Current Event Model

Applets - Applications

Applets = small applications for the browser

- placed on HTML pages

- downloaded by the browser

- executed on the browser's JVM

- severe security restrictions

- four entry point methods

- characteristics of a container for components
Gui Applications = normal applications

- execute by the interpreter

- no security restrictions

- one "static public main(String[] argv)" entry point

- need to instantiate a subclass of Frame or Window

What is AWT?

- AWT stands for "Abstract Window Toolkit"

- It is used for graphical user interfaces, windows, buttons, menus, toolbars etc.

- As data viewer to display graphics, images, graphs

- As data manipulator to alter data by direct manipulation

Why AWT?

- To create platform independent gui applications and applets

- Write once and run anywhere

- Native "look & feel"

- Native components may behave different from Java components

- JDK 1.2 contains the swing components: Plugable look & feel, platform independent, slower than AWT components


The AWT Package

Roughly divided into four parts:  


(1) Components, GUI components such as buttons, scrollbars, etc.

(2) Events, Signaling of user interaction

(3) Graphics, Colors, fonts, images, drawing, etc

(4) Layout managers, Controls the layout of the components

(1) Components


- Every part of a GUI is a Component

- Two ways to use Component type classes:
1. Assemble a GUI from existing components
2. Create new components by extending existing ones

Can you map the list above to the components on the left and right?

Basic Objects: Component

  - The Component class is the top-most class in AWT

- All the building blocks in a GUI descend from Component (except menu components)

- All AWT's primitive gui components have respective peers implemented natively

- It is not possible to instantiate Component

- Component has a very large number of methods

Basic Objects: Container

- Extends Component, i.e. a Container is a Component

- Contains components, allows grouping of components

- Since a Container is a Component, containers can contain other containers (Composite Design Pattern)

- It is impossible to instantiate Container

- Window, Dialog, Frame, Panel & Applet: all are Containers

Component Hierarchy

A First AWT Example

public class firstGUI {
   private Frame f;
   private Button b1, b2;

   public static void main(String[] args) {
      firstGUI myFirstGUI = new firstGUI();
   public void go() {
      f = new Frame("First Gui Example");
      f.setLayout(new FlowLayout());
      b1 = new Button("Press-me");
      b2 = new Button("or me! ");
      f.add(b1); f.add(b2);f.pack();


(2) Event Handling

- An event signals that something happened

- Events can be triggered by :

1. The user, using the mouse or the keyboard
2. The system, notifying changes

- Components receive events and process them accordingly

The Initial Event Model

Hierarchical, in JDK 1.02 based upon:

- containment

- propagation to the parent

Mouse Events
- Mouse events get generated when the mouse is used

- We can override several methods to provide handling:

mouseEnter (Event e, int x, int y)
mouseMove (Event e, int x, int y)
mouseExit (Event e, int x, int y)
mouseDown (Event e, int x, int y)
mouseUp (Event e, int x, int y)
mouseDrag (Event e, int x, int y)

Keyboard Events
- Similar to mouse Events

- Two main methods to overwrite

keyDown (Event e, char c)
keyUp (Event e, char c)

- We get the int value of the ASCII char
- We test for special keys by checking :

Event.xx (xx = UP, END, PGDN, etc)

An AWT Example Using The Initial Event Model

import java.awt.*;

public class AWTDemo extends Frame {
    private Panel myPanel;
    private Button myButton;   

    public AWTDemo() {
        this.setBounds(0, 0, 200, 200);
        myPanel = new Panel();
        myButton = new Button();
        myButton.setBounds(50, 50, 100, 80);
        myButton.setLabel("Click me!");

    } // end constructor
public static void main(String args[]) {
   AWTDemo myFrame;
   myFrame = new AWTDemo();;
} //end static main

// this is the old way to handle events
public boolean handleEvent(Event myEvent) {
   if ( == Event.WINDOW_DESTROY) {
      return true;
   if ( && { if (myPanel.getBackground() == { myPanel.setBackground(; } else { myPanel.setBackground(; } } //end if return super.handleEvent(myEvent); } //end handleEvent } //end class AWTDemo


The Current Event Model

Delegation based, introduced in JDK 1.1

- specialised objects named listeners handle the events

- Listener registration

* The "old" model still functions under the new model