C

Platform-Specific Event Handling

In this appendix:

  • The Results
  • Test Program

My life with Java began in September of 1995. I started on a Sun Sparc20 and have since used Java on Windows 95, Windows NT (3.51/4.0), a PowerMac, and an early version of a Java terminal. At the time I started using Java, it was in its alpha 3 release. Even before the beta release, the Internet crowd was hailing Java as the programming language for the next millennium, and people were lining up to take Sun's Java training classes.

Although Java has many important features, probably the most important is platform independence: you can compile a program once and run it anywhere. At least, that was the goal; and Java came impressively close to meeting that goal. However, there are some problems, particularly in the area of event handling. Java programs just do not act the same, from platform to platform, environment to environment. Even if you stay within Sun's Java Developer's Kit, you cannot take a program created on one platform, move it to another, and be guaranteed that it will react the same way to the user's interactions. To make matters worse, Netscape, the makers of the first run-time environment for beta API applets, Netscape, decided to take matters into its own hands with Navigator version 3.0; its version of AWT behaves slightly differently than the JDK's. On top of that, Navigator itself differs from platform to platform. Version 1.1 of the JDK introduces more idiosyncrasies, even as it resolves some others.

With more Java environments available, HotJava, Internet Explorer, and Java terminals to name a few, and new official extensions to AWT coming out, the differences are expanding, instead of contracting. Hopefully, there will be a day when this appendix can go away, completely. Until that time, I've tried to document the behavior of different run-time systems, on different platforms. If the platform is not included in this appendix, the source for a test program is. If you run the program on your platform and send the results to me at jaz@ora.com, they will be included in a future printing or provided online. The test program requires user-interaction, so please follow directions carefully. Between printings, the book's Web site will maintain the latest information at http://www.ora.com/catalog/javawt/. Only the results from using the latest releases of each platform are included in Table C-1.

C.1 The Results

Table C-1 shows the events delivered to each component on the major platforms in Java 1.0. An images in a particular entry means that the event is passed to Java from the component's peer; a dash means it is not.

Table C–1: Component Events in Java 1.0

images

images

images

images

images

Key:

IE Microsoft's Internet Explorer

HJ Sun's Hot Java Prebeta 1

JDK

Java Developer's Kit 1.0.2 (appletviewer/Java)

NN

Netscape Navigator

SDK

Microsoft SDK

Sun

Solaris 2.x (UNIX/Motif)

Yes, things changed again with the 1.1 release. Table C-2 shows which Java 1.0 events are generated for each component in Java 1.1. Fortunately, there is one clear improvement: the Java 1.1 event model promises much more uniform event processing, since it's largely under your control. For example, you can attach a MouseListener to a Label and receive mouse events that would not be generated with the 1.0 event model.

Table C–2: Java 1.0 Component Events in Java 1.1

images

images

images

images

Key:

HJ Sun's Hot Java Prebeta 2

JDK

Java Developer's Kit 1.1 (appletviewer/Java)

Sun

Solaris 2.x (UNIX/Motif)

C.2 Test Program

The test program, compList, listed in Section C.2.2 shows the events peers pass along to the Java run-time system. You can then examine the output to see how the run-time system reacts to the different events. When you run compList, the screen looks something like the one in Figure C-1.

images

Figure C–1: Test program

C.2.1 How to Use the Program

Java does not have an automated record and playback feature, so the work is left for you to do. The program displays 10 components: Label, Button, Scrollbar, List, multiselection List, Choice, Checkbox, TextField, TextArea, and Canvas (the black box in Figure C-1). Basically, you must manually trigger every event for every component.

For every component on the screen (except Done), do the following:

With the mouse

Move the cursor over the object, press the mouse button and release, and drag the cursor over the object.

With the keyboard

Press and release an alphabetic key, press and release the Home and End keys, arrow keys, and function keys. Do this for every component, even for components like Button and Label that have no logical reason for using keyboard events.

For items with choices

Select and deselect a few choices; double-click and single-click selections.

For the scrollbar

Click on each arrow, drag the slider, and click in the paging area (the space between each arrow and the slider).

For the text field

Press Enter.

When finished

Press the Done button, and analyze the results. Run the program again (without exiting), and check the results again. Try to trigger any specific events that you expect but didn't appear in the output from the first pass. Generating some events requires a little work. For example, on a Macintosh, in order to get the MOUSE_UP and MOUSE_DRAG events, you must do a MOUSE_DOWN off the component; otherwise, the MOUSE_DOWN/MOUSE_UP combination turns into an ACTION_EVENT, if that component can generate it.

NOTE

The SunTest business unit of Sun Microsystems has an early version of a record and playback Java GUI testing tool called JavaSTAR. Information about it is available at http://www.suntest.com/JavaSTAR/JavaSTAR.html. In the future, it may be possible to use JavaSTAR to help automate this process.

C.2.2 Source Code

The following is the source code for the test program:

import java.awt.*;
import java.util.*;
import java.applet.*;
public class compList extends Applet {
    Button done = new Button ("Done");
    Hashtable values = new Hashtable();
public void init () {
    add (new Label ("Label"));
    add (new Button ("Button"));
    add (new Scrollbar (Scrollbar.HORIZONTAL, 50, 25, 0, 255));
    List l1 = new List (3, false);
    l1.addItem ("List 1");
    l1.addItem ("List 2");
    l1.addItem ("List 3");
    l1.addItem ("List 4");
    l1.addItem ("List 5");
    add (l1);
    List l2 = new List (3, true);
    l2.addItem ("Multi 1");
    l2.addItem ("Multi 2");
    l2.addItem ("Multi 3");
    l2.addItem ("Multi 4");
    l2.addItem ("Multi 5");
    add (l2);
    Choice c = new Choice ();
    c.addItem ("Choice 1");
    c.addItem ("Choice 2");
    c.addItem ("Choice 3");
    c.addItem ("Choice 4");
    c.addItem ("Choice 5");
    add (c);
    add (new Checkbox ("Checkbox"));
    add (new TextField ("TextField", 10));
    add (new TextArea ("TextArea", 3, 20));
    Canvas c1 = new Canvas ();
    c1.resize (50, 50);
    c1.setBackground (Color.blue);
    add (c1);
    add (done);
}
public boolean handleEvent (Event e) {
    if (e.target == done) {
        if (e.id == Event.ACTION_EVENT) {
            System.out.println (System.getProperty ("java.vendor"));
            System.out.println (System.getProperty ("java.version"));
            System.out.println (System.getProperty ("java.class.version"));
            System.out.println (System.getProperty ("os.name"));
            System.out.println (values);
        }
    }else {
        Vector v;
        Class c = e.target.getClass();
        v = (Vector)values.get(c);
        if (v == null)
            v = new Vector();
        Integer i = new Integer (e.id);
        if (!v.contains (i)) {
            v.addElement (i);
            values.put (c, v);
        }
}
        return super.handleEvent (e);
    }
}

An HTML document to display the applet in a browser should look something like the following:

<APPLET code="compList.class" height=300 width=300>
</APPLET>

C.2.3 Examining Results

The results of the program are sent to standard output when you click on the Done button. What happens to the output depends on the platform. It may be sent to a log file (Internet Explorer), the Java Console (Netscape Navigator), or the command line (appletviewer). The following is sample output from Internet Explorer 3.0 on a Windows 95 platform.

Microsoft Corp.
1.0.2
45.3
Windows 95
{class java.awt.Canvas=[504, 503, 1004, 501, 506, 502, 505, 1005,
401, 402, 403, 404], class java.awt.Choice=[1001, 401, 402, 403,
404], class java.awt.Checkbox=[1001, 402, 401, 403, 404], class
compList=[504, 503, 501, 506, 502, 505, 1004, 1005], class java.
awt.TextField=[401, 402, 403, 404], class java.awt.List=[701,
1001, 401, 402, 403, 404, 702], class java.awt.Scrollbar=[602,
605, 604, 603, 601], class java.awt.TextArea=[401, 402, 403, 404],
class java.awt.Button=[1001, 401, 402, 403, 404]}

In addition to some identifying information about the run-time environment, the program displays a list of classes and the events they passed. The integers represent the event constants of the Event class; for example, Canvas received events with identifiers 504, 503, etc. The events are not sorted, so you can see the order in which they were sent. Unfortunately, you have to look up these constants in the source code yourself. The class listed as compList is the applet itself and shows you the events that the Applet class receives.

Get Java AWT Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.