To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98
CHAPTER 38 User Input and Interactivity with Java
by Mike Morgan
- In this chapter
- Interacting with People by Using Java 1026
- Using the Abstract Windowing Toolkit 1027
- Working with Layouts 1038
- The Swing Architecture 1052
- Swing Component APIs 1055
- Using Swing-Specific Layouts 1068
- Swing Listeners and Events 1069
Interacting with People by Using Java
User interaction with your programs is by far the most important aspect of creating Java applets and applications. If your interface is bad, people will not want to use your program. Come up with an intuitive interface, however, and you could have the next killer app. But keep in mind that building those awesome interfaces requires you to start with some foundation.
It is that Java foundation that this chapter discusses. Learning about the different tools at your disposal enables you to build intuitive interfaces that empower your users.
The Abstract Windowing Toolkit
The Abstract Windowing Toolkit (AWT) was Javas primary user interface component package in JDK 1.0 and 1.1. You can still use the AWT, but now Sun has also introduced a new package code-named Swing. These packages contain all the programming tools you will need to interact with the user. This section describes the now-classic AWT tools; the next section describes Swing.
The AWT contains a number of familiar user interface elements. Figure 38.1 shows a Java applet with a sample of some of the components of the AWT.
FIGURE 38.1 The AWT features a number of familiar components.
Figure 38.2 shows a portion of the AWTs inheritance hierarchy.
The original AWT in version 1.0 of the JDK did its work by instantiating peer objects from the native operating system. Suppose you asked Java to put a button on the screen, for example, and then ran your application on a Windows machine. The Java runtime would handle your request by asking Windows to make a new button. After that, your Java object and the corresponding Windows object would communicate about mouse clicks and other events in order for the button to behave as you expected.
FIGURE 38.2 The AWT inherits all its user interface components from Component.
Java Foundation Classes and the Swing Components
Starting in JDK 1.1, Sun made substantial changes to the user interface components. Figure 38.3 illustrates the current state of affairs. The original AWT from JDK 1.0 is gone; in its place is the AWT that was introduced in JDK 1.1, as well as a new interface called Swing. (Swing, in turn, is part of the Java Foundation Classes, or JFC.) Java user interface classes that do their work through peer objects are still available; theyre called heavyweight components now. As you might guess, there are also lightweight components that dont tie you to the peer objects of the native operating system. Instead, they support pluggable look and feel, enabling an end user to select from a Windows, Macintosh, or native Java appearance (among others).
NOTE: In addition to pluggable look and feel, the JFC offers printing capability, clipboard data transfer, improved accessibility for people with disabilities, and much more. Its a major upgrade and well worth the time it takes to learn it.
FIGURE 38.3 A revised version of the AWT is still available in the Java Foundation Classes.
Using the Abstract Windowing Toolkit
As a platform-neutral language, Java makes many of the details of graphical user interface (GUI) programming invisible, but they dont go away. As we saw in Chapter 37, Developing Java Applets, you can build a simple applet with a GUI interface in just 10 lines of code. (See Listing 37.2, HelloApplet.java.) It takes just a few more lines to add a main() routine and a standalone Frame, enabling the applet to double as a GUI application. (Listing 38.1 shows such a program.)
Listing 38.1 HelloApplication.javaIf Youre Going to Write an Applet, Consider Adding the Code to Make It a Standalone GUI Application as Well
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class HelloApplication extends Applet {
public static void main(String[] args) {
HelloApplicationFrame theApplication =
new HelloApplicationFrame(Hello Application);
theApplication.setSize(200,200);
theApplication.show();
}
public void paint(Graphics theGraphics) {
theGraphics.drawString(Hello, World!, 0, 50);
}
}
class HelloApplicationFrame extends Frame {
private HelloApplication fApplet;
public HelloApplicationFrame(String name) {
super(name);
addWindowListener(new HelloWindowAdapter());
fApplet = new HelloApplication();
fApplet.init();
fApplet.start();
add(fApplet);
}
class HelloWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
fApplet.stop();
fApplet.destroy();
System.exit(0);
}
}
In this first section, well take some time to understand how this program works; then well use it as the foundation for building more sophisticated user interfaces with the AWT.
To learn more about programs that can run both as an application and as an applet, compile HelloApplication.java and experiment with it in both environments. (To use HelloApplication as an applet youll need an HTML file. The one in Listing 38.2 works well.) Figure 38.4 shows this program in action.
Listing 38.2 HelloApplication.htmlYoull Need an HTML File with an <APPLET> Tag in Order to View HelloApplication as an Applet
<HTML>
<BODY>
<APPLET CODE=HelloApplication.class
WIDTH=200" HEIGHT=200">
</APPLET>
</BODY>
</HTML>
FIGURE 38.4 HelloApplication can be run as an applet or as an application.
Understanding Components and Containers
You can write a one-line main() method and get a command-line application started. In order to build a GUI program, however, someone needs to provide a place in which to draw. (In GUI programs all elements of the interface are drawneven text.) If youre writing an applet, the browser is responsible for that detail. If you want your program to function as an application you have to do that work yourself.
In AWT, the place in which to draw is called a container, and is derived from the Java java.awt.Container class. The elements of the user interface itself are called componentsthey all derive from java.awt.Component. Some of the components supplied with the AWT include:
- java.awt.Button
- java.awt.Canvas
- java.awt.Checkbox
- java.awt.Choice
- java.awt.Container
- java.awt.Label
- java.awt.List
- java.awt.Scrollbar
- java.awt.TextComponent
- java.awt.Composite
|