by David Wood

This week, we're going to be examining action and reaction, so to speak.

As we mentioned last time, the heart of the Java User Interface is based entirely in objects; every button, text box, slider and label are defined as classes in the Java Abstract Window Toolkit. They're instantiated as objects, and you manipulate them as such. Not surprisingly, the "events" we've been discussing are, typically, related directly to objects we define and create within our interface.

Not to say that every program is typical; there are events available to us whether we have created an interface or not, and depending on what we're doing, they may or may not be useful. To start, however, we're going to study the event in the context of a discreet user interface "Component" - the ubiquitous Button.

Owing to the fact that Java has been set up with a great deal of intelligent "defaults", it's very easy, for example, to create a button. You simply define a variable of type java.awt.Button, assign it an instance of the Button class, and add ( ) it to your interface. Like so:


   ...

     Button mybutton;

     mybutton = new Button();

     mybutton.setLabel ("I'm a Button");

     add (mybutton);

   ...

and the button appears. Believe us when we say that organizing and creating your interface can be much more complex and intricate than this; it's possible to specify a great deal more about how interface objects relate to each other, from their appearance on the screen to the hierarchy in which they are arranged. In fact, the bit of code we've quoted above assumes without stating explicitly the presence of both a Window and a Panel, which are two central organizing principals of the Java interface. Without going into too much detail, the Window is a "Frame" that exists automatically for Applets (that's their space on the browser's screen), and within every Frame exists a primary Panel, inside of which other panels can be created in hierarchies as complex as you can imagine.

Additionally, there are Layout Managers which govern the precise geometry of how objects are placed in the window. By not explicitly using one, we are defaulted to the simple but usually effective FlowLayout system.

Notice the way we set what the button will say? It's simply calling a method within our newly created button object that makes the appropriate change.

So how does this creation go from screen decoration to useful channel for user input? Simple; the action ( ) method that we mentioned previously. To illustrate, here is a new rendition of our FirstTry applet called SecondTry, this time achieving the same effect with a Button and an action ( ) definition.



import java.awt.*;



public class SecondTry extends java.applet.Applet {



   Button b1;



   public void init ( ) {

      b1 = new Button ( );

      b1.setLabel ("Please Click Me!");

      add (b1);

   }



   public boolean action (Event evt,Object what) {

      if ((evt.id & evt.MOUSE_DOWN) != 0) {

         b1.setLabel ("Thank You!");

         return (true);

      } else {

         return (false);

      }

   }

}



We use the init ( ) method of our applet to create the button and "add" it. Then we define the action ( ) method.

This method is executed whenever there's an action of some kind on one of our interface objects. The "action routine" simply checks to see whether or not the action it's handling is a "MOUSE_DOWN" - a mouse click. If it is, as determined by checking the result of an AND operation between the MOUSE_DOWN and the actual event, we once again call our button's setLabel method and change the text in the button. It's also important that we return a true or false value depending on whether or not we successfully "intercepted" the action.

The action ( ) method provides us with Event object data (as discussed last week) as well as a variable of type Object, which indicates, somewhat redundantly, what kind of action is occurring. If we needed to distinguish between different types of events, perhaps occurring on different objects, it would be easily possible to do so.

It should begin to be apparent from this how various interface objects can be tied in with a well written action method to create very efficient control over a running applet. Naturally, this is only one way to go about communicating with your users, and once again, this is only the tip of the iceberg for the functionality we've introduced. We recommend perusing the Java tutorial's user interface overview, and checking out the interface object examples in the tutorial guide to AWT components. Next week, we'll be going yet a little bit deeper into event handling, and we'll focus on some vastly more interesting features of the interface package, so be sure to tune in.

Past installments of Java Jolt

http://www.internet.com/