Introduction to Object Oriented Programming, 3rd Ed

Timothy A. Budd

Chapter 22

The AWT and Swing


  1. Roadmap
  2. Classes Frame and JFrame
  3. Panel and JPanel
  4. Layout Manager
    1. Showing the Use of a Layout Manager
  5. Event Listeners
    1. Adapters Reduce Code Size
    2. Creating a Listener
  6. Buttons, Sliders, Text boxes and so on
    1. Combining Component and Listener in one Class
  7. Chapter Summary

Other Material

Intro OOP, Chapter 22, Outline


In this chapter we will examine the Java AWT library for graphical user interfaces, noting in particular how it uses the framework concepts developed in the last chapter.
Intro OOP, Chapter 22, Slide 01

Classes Frame and JFrame

The place to begin is the class Frame (in AWT) or JFrame (in Swing). These give the overall window for the application, and hold features such as the title bar and menu bar. This is a straightforward implementation of the framework ideas discussed in the previous chapter.
Intro OOP, Chapter 22, Slide 02

The Swing library was introduced in Java 1.3, and has slowly replaced the older AWT library.

Panel and JPanel

The window is combined with an instance of class Panel (JPanel in Swing)

This class is the screen on to which drawing operations are directed.

JPanel can include components (buttons and the like) and can refine the method paint.
(The overridden method must invoke the parent method, in order to render features of the window not included in the user constructed portions).

class MyPanel extends JPanel {
	public void paint (Graphics g) {
Intro OOP, Chapter 22, Slide 03

Layout Manager

Here is a problem.
Intro OOP, Chapter 22, Slide 04

Showing the Use of a Layout Manager

class MyPanel extends JPanel {
	public MyPanel () {
		setLayoutManger (new BorderLayout());
Both JPanel and BorderLayout are part of the framework, not written by the end programmer. The end programmer selects the appropriate type of layout manager. In this fashion the framework provides a flexible solution, without making the frame class overly large. (As it would be if it needed to hold all layout managers).
Intro OOP, Chapter 22, Slide 05

Event Listeners

Event listeners are another good example of the use of inheritance and overriding in the Java AWT framework.

Every component (window, button, scroll bar) maintains a list of ``listeners''; objects that are interested in being notified when an event has taken place.

Each type of event is defined by an interface, such as the following:

public interface MouseListener extends EventListener {
	public void mouseClicked (MouseEvent e);
	public void mouseEntered (MouseEvent e);
	public void mouseExited (MouseEvent e);
	public void mousePressed (MouseEvent e);
	public void mouseReleased (MouseEvent e);
Intro OOP, Chapter 22, Slide 06

Adapters Reduce Code Size

More often than not, programmers are interested in only one or two events, not all the possible events defined by the interface.

An adapter provides empty implementations, allowing the programmer to redefine only the events of interest.

public class MouseAdapter implements MouseListener {
	public void mouseClicked (MouseEvent e) { }
	public void mouseEntered (MouseEvent e) { }
	public void mouseExited (MouseEvent e) { }
	public void mousePressed (MouseEvent e) { }
	public void mouseReleased (MouseEvent e) { }
Intro OOP, Chapter 22, Slide 07

Creating a Listener

The programmer then creates a new class that subclasses the adapter and overrides the methods of interest, then registers an instance of the class with the component.
class MyWindow extends JFrame {
	public MyWindow () {
		addMouseListener (new MouseKeeper());
	private class MouseKeeper extends MouseAdapter {
		public void mousePressed (MouseEvent e) {
Inner classes are particularly usefor for this idiom.
Intro OOP, Chapter 22, Slide 08

Buttons, Sliders, Text Boxes, and so on

Graphical components (Buttons, Sliders, Text Boxes, and many many more) are provided in the standard library.

The user places an instance of a class into the window. Most use the listener event model to notify when they are changed.

	Button butn = new Button ("do it!");
	add("North", butn); // place at top of screen
	butn.addActionListener (new doIt()); // add listener
	private class doIt implements ActionListener {
		public void actionPerformed (ActionEvent e) {
			// what ever do it does
The listener model avoids the need for inheritance from the component class.
Intro OOP, Chapter 22, Slide 09

Combining Component and Listener in one Class

Sometimes, a better encapsulation results from combining inheritance from the component class with implementation of the listener. The component then becomes its own listener.
class ColorButton extends Button implements ActionListener {
	private Color ourColor;

	public ColorButton (Color c, String name) {
		super (name); // create the button
		ourColor = c; // save the color value
		addActionListener (this); // add ourselves as listener

	public void actionPerformed (ActionEvent e) { 
			// set color for middle panel
		setFromColor (ourColor); 
Intro OOP, Chapter 22, Slide 10

Chapter Summary

The AWT illustrates the application of the framework concept at many different levels. In this chapter we have examined