Here's a small program that illustrates a couple of nifty new features of JDK 1.1.x. The program itself simply creates a frame with a button in it. The neat thing is how it's created. Some interesting features of this code snipped include:

  1. The class tomButton is an inner class, created within the tomFrame class.
  2. The JDK 1.1 event model is used to handle events
  3. The event handlers are created as anonymous inner classes

Note: This will not compile under JDK 1.0.2.

To bone up on inner classes, see the 1.1 supplement to the Java Language Specification.

Note that when you compile this example, you'll get several strangely-named .class files. The file named "tomFrame$tomButton.class" is the class file for the inner class "tomButton". The files named "tomFrame$1.class" and "tomFrame$1.class" are the anonymous inner classes created to handle events.

Briefly, the JDK 1.1 event model is a "callback"-type event model. If you're interested in responding to an event, you create an EventListener of the appropriate type (this program creates a WindowListener for the frame and an ActionListener for the button), and call the target object's addSomethingListener() method (where Something is the type of event; for example, addActionListener()). From then on, whenever an event of the appropriate type is called, the corresponding method in your EventListener class will be called. This is a lot tidier than the big ugly case statements we're all used to from JDK 1.0.2. (On the other hand, to speak of a truly big and ugly case statement, you must try Windows programming. :-)).

Anyway, here's the source