Session 21

Polymorphism in the AWT

Computer Science II
Object-Oriented Programming

Drawing an Image

My java snippet for drawing an image turned out to be quite timely for my graduate alma mater. Onward to the Final Four!

Opening Exercise

Write a Java class ButtonDemoFrame to create this window:

border layout manager demo

Whenever the user presses a button, the program writes the name of the button to the standard output.

Toward a Solution

We encounter several design questions in writing this program::

Here is a quick solution: DemoButtonListener and ButtonDemoFrame.

Can we make the duplication go away? Creating and setting up the five buttons repeats a lot of code. What differs among the five button creation snippets?

Sure -- extract a method to make the buttons: ButtonDemoFrame.


Java's Abstract Window Toolkit

Today we will consider a bit more about how to build graphical interface components using Java's Abstract Window Toolkit, or AWT. The AWT shows you lots of examples of polymorphism in the Java libraries.


Text Fields and Areas

Suppose that we would like to define a window that looks like this:

text field and area demo

The user is able to type text into each box independently of the other.

Let's write a Java class called TextEntryFrame... With the AWT's support, this turns out to be almost too easy:

    import java.awt.Frame;
    import java.awt.TextField;
    import java.awt.TextArea;

    public class TextCheckFrame extends Frame
        public TextCheckFrame()
            setTitle( "Text Component Examples" );
            setSize ( 300, 250 );

            add( "North", new TextField() );
            add( "South", new TextArea () );

This solution comes with a new exercise built right into it:

Suppose that we want any text typed into the text field to appear in the text area, too.

What changes do we have to make?

"What do you mean by that?", you ask?

interacting field and listener 1 interacting field and listener 2

interacting field and listener 3 interacting field and listener 4

interacting field and listener 5

Now our solution needs a custom listener...

The AWT as a Framework

The AWT is a framework, a set of classes that provide objects and control for building graphical applications. It provides built-in control that works in your applications by taking advantage of polymorphism through inheritance and interfaces.

Consider a typical message sequence in one of our ...BallWorld programs:

message trace

The show() message example exemplifies the Hollywood Principle:

Don't call us; we'll call you.

We use inheritance and polymorphism to allow programmers to create complex applications with as little work as possible, overriding only those methods that need special behavior.

Methods in a framework that you must override are called hot spots and are the key to learning and using a framework. They should also be the focus of documentation for a framework.

Working with a framework introduces some new problems for us to watch for:

Dealing with the yo-yo problem is mostly a matter of practice. Once you've learned a framework or two, bouncing up and down the class hierarchy will seem natural.

Dealing with the tooth fairy problem is mostly a matter of good documentation and asking questions when you encounter unexpected behavior -- or, more likely non-behavior.

Wrap Up

Eugene Wallingford ..... ..... March 29, 2005