Programming User Interfaces using the AWT (and the JFC)

Jan Newmarch jan@newmarch.name

Last modified: 10 February, 1998. These slides are Copyright Jan Newmarch, 1995, 1996, 1997, 1998.


Abstract

This tutorial discusses using the AWT (Abstract Window Toolkit) of Java to perform platform-independent GUI programming.

It makes the following assumptions:


Contents


GENERAL CONCEPTS


GUI programming with Java


Evolution of Java GUI programming


Applet vs Application


AWT Hierarchy


AWT Primitive Components

The main ``primitive'' objects derived from Component are

These are all in package java.awt.

Swing Primitive Components

These mirror the AWT components:


Container Components

Container objects are derived from Component. The main ones derived from that are

MenuComponents

Menus are implemented by subclassing MenuComponent which is derived from Object

Layout Objects

Layout objects derive from Object and are used for geometry management

COMPONENTS


Component Objects


Label


Label (2)


Hello World

Hello World using a Label in a Frame is
import java.awt.*;

public class Hello extends Frame {
  public static void main(String argv[])
  {
    new Hello();
  }
  Hello() {
    Label hello =
             new Label("Hello World");
    add(hello, "Center");
    setSize(200, 200);
    setVisible(true);
  }
}

JLabel


Frame


JFrame


Hello World using Swing

import com.sun.java.swing.*;

public class JHello extends JFrame {

  public static void main(String argv[])
  {
    new JHello();
  }

  public JHello() {
    JLabel hello =
              new JLabel("Hello World");
    getContentPane().add(hello, "Center");
    setSize(200, 200);
    setVisible(true);
  }
}

Button


JButton


EVENTS


AWT events


AWT Event classes

The class AWTEvent has subclasses:

AWT Event ids

Some of these classes have an id value to distinguish between them

Triggering events

Events are posted by user actions (or may be done programmatically)

Listeners

Listeners are objects that handle events (not the GUI Components any more)

Listeners as interfaces

Listeners are defined as interfaces and must be implemented by the application
public interface ActionListener
       extends EventListener {
  public void actionPerformed(ActionEvent e);
}

public interface MouseListener
       extends EventListener {
  public void mouseClicked(MouseEvent e);
  public void mousePressed(MouseEvent e);
  public void mouseReleased(MouseEvent e);
  public void mouseEntered(MouseEvent e);
  public void mouseExited(MouseEvent e);
}

Registering listeners


Simple Delegation program (1)

import java.awt.Button;
import java.awt.Label;
import java.awt.Frame;
import java.awt.event.ActionListener;

public class DelegateDemo extends Frame {
  public static void main(String argv[]) {
    new DelegateDemo().setVisible(true);
  }

  public DelegateDemo() {
    // create the GUI objects
    Button left = new Button("Left");
    Button right = new Button("Right");
    Label label = new Label("       ", Label.CENTER);

    // set their geometry
    add(left, "West");
    add(right, "East");
    add(label, "Center");
    pack();


    // create a listener and add it to each Button
    SimpleListener simple = 
	new SimpleListener(label);
    left.addActionListener(simple);
    right.addActionListener(simple);	
  }
}



/**
 * A listener object that is invoked when a Button is activated
 * It finds the Button's label and sets it in a Label
 */
class SimpleListener implements ActionListener {

    private Label label;

    public SimpleListener(Label l) {
	// the listener needs to know the Label it will act on 
	label = l;
    }

    public void actionPerformed(java.awt.event.ActionEvent e) {
	// get the label showing in whichever Button was pressed
	String name = e.getActionCommand();

	// set this in the Label object
	label.setText(name);
    }
}	

Some variations


Motion Tracking (1)


Motion Tracking (2)

import java.awt.*;

public class TrackResize extends Frame {
 
  public static void main(String argv[]) {
    new TrackResize().setVisible(true);
  }

  public TrackResize() {
    Label label = new Label();
    add(label);	
    setSize(300, 100);

    addComponentListener(new Tracker(label));
  }
}


class Tracker implements ComponentListener {
    private Label label;

    Tracker(Label l) {
	label = l;
    }
    public void componentHidden(ComponentEvent e) {
	// empty
    }

    public void componentMoved(ComponentEvent e) {
	showGeometry(e);
    }

    public void componentResized(ComponentEvent e) {
	showGeometry(e);
    }

  // Tracker continues
  public void componentShown(ComponentEvent e) {
    // empty
  }

  private void showGeometry(ComponentEvent e) {
    Component c = e.getComponent();
    Dimension d = c.getSize();
    Point p = c.getLocation();

    label.setText("Position: (" + p.x +
  	          "," + p.y + ") Size: )" +
		  d.width + "," + d.height + ")");
  }
}

Changing key values (1)

import java.awt.*;
import java.awt.event.*;

public class MapKey extends Frame {
  public static void main(String argv[]) {
    new MapKey().setVisible(true);
  }
  public MapKey() {
    TextField text = new TextField(20);
    add(text);	
    pack();
    text.addKeyListener(new ToUpper());
  }
}

Changing key values (2)


class ToUpper implements KeyListener {
    
  public void keyTyped(KeyEvent e) {
    // empty
  }

  public void keyPressed(KeyEvent e) {
    e.setModifiers(Event.SHIFT_MASK);
  }

  public void keyReleased(KeyEvent e) {
    // empty
  }
}

Consuming events


Consuming events

This key listener discards non-alphabetic events:
public class Alpha implements KeyListener {
  public void keyPressed(KeyEvent e) {
    if (! Character.isLetter(e.getKeyChar())) {
      Toolkit.getDefaultToolkit().beep();
      e.consume();
     }
  }
  public void keyReleased(KeyEvent e) {
    // empty
  }
  public void keyTyped(KeyEvent e) {
    // empty
  }
}

Generating Events


Generating Events of a new Class


FileDialog


FileDialog (2)


FileDialog Parent

Creation of a FileDialog requires a Frame parent. If this is done from a Button then the Frame has to be found by walking up the parent tree from the Button:
class FileOpen implements ActionListener {
   public void actionPerformed(ActionEvent e) {
      Object fr = e.getSource();

      while (fr != null && 
	     !(fr instanceof Frame))
	 fr = ((Component) 
		    fr).getParent();
      new FileDialog((Frame) fr, "Open");
      // etc
   }
}

JFileChooser



All material on this site is under the copyright restriction and permissions of the Open Content license, http://www.opencontent.org/opl.shtml