The content on this page is the work of Professor Glenn Blank.Be advised that Professor Blank is no longer on the active faculty at Lehigh.This content continues to be available as a courtesy, but it may not be maintained or current.

Most GUI class libraries in C++ are platform specific, not just because of
different hardware capabilities,
but subtle differences between the "look-and-feel"
of various Windowing operating systems.
The Java Abstract Window Toolkit provides a cross-platform library
which seeks to observe look-and-feel
conventions of various OS platforms.

Container is an abstract
class:
I.e., it cannot be instantiated; some of its methods must be
implemented by subclasses.
Nevertheless, Container does implement some useful methods,
including:

public Component add(Component comp); //put a Component into a Container
public setLayout(LayoutManager mgr); //lay out components in some pattern

A Window
object is a top-level window with no borders and no menubar.
It can generate a WindowOpened or a WindowClosed
event,
to which a WindowListener or WindowAdapter can respond. (We'll
discuss events further below.)

A Frame
is a top-level window with a title and a border.
Because it has more features, it can generate more events: WindowOpened, WindowClosing, WindowClosed, WindowIconified, WindowDeiconified,
WindowActivated,
WindowDeactivated.
Again, respond to these events with
a WindowListener
or WindowAdapter.

Once a subclass of Container,
such as Window
or Frame
or Panel,
has been constructed,
it can then invoke an add method to attach any AWT component
within it,
such as a Button or a Label or a TextField or another Frame or Panel.

A simple example

Here's a simple program (Ex_1.java)
which constructs a Frame
and an unresponsive Button:

//program to demonstrate the construction of a Container and a Button
import java.awt.*;

The next example (Ex_3.java)
adds the ability to responds to events, such as a mouse button click
Uses the event delegation model of
JDK 1.1
When an event occurs, when a Button
is pressed, an ActionEvent
objects is generated
The ActionListener interface listens for a
particular ActionEvent
and responds in its actionPerformed
method
The WindowListener interface listens for
events associated with Window objects,
such as
closing a window, and responds in corresponding methods

The program now has a live
button, whose actionPerformed
method rings a bell,
as
well as a live close window button, which performs System.exit(0)
There are listeners associated with
most Components
in the AWT.
Note: if you implement WindowListener
interface, you have to define all of the methods in it.
Alternatively, the abstract class WindowAdapter defines null methods for them all,
so you can
only have to define methods for events you care about, such as windowClosing.

Object source
provides the selected menu item, such as a name of a color from the COLORS
menu

method itemStateChanged()
responds to pressing a radio button

next several methods use Graphics
methods to implement drawing capabilities of Sketchpad

including
implementation of mouse listener methods

Finally, class Ex_7 is the
main()
method, which simply constructs a Sketchpad and sets it visible

Layout Managers

Sketchpad uses hard-coded layout, which depends on a 800x600 screen
JDK also provides a set of generic layout
manager classes
which
arrange Component objects within a Container object in various predictable ways

FlowLayout
(the default) add components one after another in rows, e.g.

BorderLayout
arranges components using along four sides (North, South, East West) and
Center positions

Applets

So far, these notes have looked at stand-alone applications using Java AWT
On the web, Java programs are called Applets
class java.applet
is a subclass of Panel,
designed to run within a Web browser

For security reasons, Applets are forbidden many
of the capabilities of applications,
such as any
file access, whether read, write or executing programs on a disk,
or network
socket connections anywhere but the server of the applet
Nevertheless, applets have a fair amount of capability,
since they are written in a general purpose programming language

Of course, Web browsers read HTML, so to run an applet,
you create HTML code which specifies an applet:

HelloWorld.class
is the Java byte code compiled from the source program HelloWorld.java
the paint() method displays information in
an applet's window (note that this is no main() method)
its parameter, Graphics g, makes
graphics capability available to an applet.

Since class java.applet
inherits from Panel and Container, it has all the capabilities of the AWT.
RadioButtons.java illustrates radio buttons and font control in an applet (run in browser).

Class java.applet
also has a number of methods available for manipulating it size, web links and
media content.

netlinks.java
illustrates links to other web sites (run in browser)
uses java.net package
to connect to and communicate with other sites on the Internet
1) This program sets up arrays of web site names and corresponding URLs
2) Next, it creates an array of Buttons, each with the name of a web site
3) Method actionPerformed
responds to a Button by getting the corresponding URL getAppletConext().showDocument(site);
//retrieves page at a URL site

LoadAudioAndPlay.java illustrates playing sounds in
an applet (run this program in browser)
uses class AudioClip, which
is part of the Applet
package, to declare a variable sound: AudioClip
sound;
uses Applet method getAudioClip to
load a sound from a web page: sound = getAudioClip(getCodeBase(),"audio\\"+source.toString()+".au");
uses getCodebase() to
return the URL from which the applet's code was loaded
and finally plays
the AudioClip: sound.play();
Until recently,
the JDK only supported the .au
format,
but support for wav and aiff and midi is coming with JavaSound in
the Java Media Framework.

DeitelLoop.javaillustrates
animation of GIF files in an applet (run this program
in browser)
This version
simply loads a set of graphics into an array, then displays them in order. DeitelLoop5.java
illustrates techniques to improve the performance of animation in an applet
(run this program in browser)
This version
lets you test the effect of changing the sleep time between displaying each
image
(by changing a HTML parameter -- try running with
different parameters)
class MediaTracker determines
when an image is completely loaded,
until tracker.imageImage()
has loaded image before playing it in animation.
This version
also uses double-buffering, i.e., drawing to memory off screen to avoid
flicker:
see buffer
and gContext.
And this
version also uses threads (by implementing the Runnable interface), to permit a user
to stop the animation by leaving the page.

Separate user interface logic from "business logic," i.e., the
problem that you are trying to model.
AWT 1.1 "listeners" were designed for this
purpose; inner classes facilitate it furtherSeparation.java
example illustrates this approach: class
BusinessLogic knows nothing about UI; class
Separation keeps track of all UI details and talks to BusinessLogic
through its public interface.

How does this design loosely coupled?
How does it support reuse?
How does it support legacy code?

Also note use of inner classes for all "listeners" nested within Separation
Contrast code of badidea1.java:
look at code in actionPerformed:

Why is the cascaded if above a bad idea? (Apparently many textbooks encourage this ugly
practice, probably betraying influence of AWT 1.0?)

Suggestion: to eliminate flicker, you may want to override update():public
void update(Graphics g) { paint(g); }
Overrides default version of update() which clears the background,
then calls paint()
to redraw any graphics

Introduction to Swing

Swing appeared after JDK 1.1, but itís designed to work with JDK 1.1 as an
add-on; it comes with JDK 1.2.
Sun has an
on-line tutorial. Swing includes lots of features that users have to expect
from a GUI:

Support for dynamically
"pluggable look and feel" (as of now, Windows and Motif)

Plus: lightwight components
written entirely in Java for portability

In C:\java\jdk1.2\demo\jfc\SwingSet, run java SwingSet

Under Options, select Windows
Look and feel

Manipulate Buttons, etc.

Much JDK 1.1 code is easily ported to Swing: simply add a prefix
"J" to the classes: e.g., JButton for Button.
However, Swing adds a number of new types of buttons and changes the way they
are organized:
All buttons, checkboxes, radio buttons and even menu items are inherited from AbstractButton.