Custom functionality may also be included in the shadow classes. For
example, the window shadow resizes the window to its preferred size
and maps the window when its visible
attribute is set to
true.
Groups that are included inside other groups are called subgroups. The top-level group is called the base group. A tree of groups exists while the application is running. Messages sent by subgroups propagate up toward the top of the tree until they are handled or they reach the base group.
The interface to a group consists of a set of attributes and methods. The class Group defines a set of attributes and methods. Specific subclasses of the Group class add their own customized attributes and methods. The attributes and methods of the shadows and subgroups contained by a group are not automatically inherited.
Applications created using GUI builder are actually groups. A large application typically consists of a base group that contains many other shadows and subgroups.
When writing a group, many of your tasks involve setting and getting the attributes of the shadows contained by the group. Two methods are used to manipulate attributes:
public void set(String key, Object value); public Object get(String key);
Example:
gui.OK.set(, );
String text = (String)gui.OK.get();
NAME TYPE DEFAULT VALUE ---- ---- ------------- name java.lang.String null visible java.lang.Boolean TRUE enabled java.lang.Boolean TRUE foreground java.awt.Color null background java.awt.Color null font java.awt.Font null
The button shadow defines an additional attribute:
text java.lang.String button
In future releases the list of attributes will automatically be
extracted from a shadow or group. This will make it possible to
create a document listing all the attributes for each shadow class.
The visible
attribute has a special behavior. When
visible
is set to true on a window for the first time,
the window changes to its preferred size before display. Subsequent
setting of the visible
attribute to true or false will cause
the window to be shown or hidden, but will cause no further resizing
of the window.
AWT stands for Abstract Windowing Toolkit. AWT is the GUI toolkit
that is included with the JDK. This document assumes some familiarity
with AWT, especially in the area of AWT events. For
information on AWT, please refer to the JDK documentation.
A message has the following fields:
String nameName of the event; this is "AWT" for AWT events
Object argSame as the
arg
field in the AWT event
Object targetThe shadow corresponding to the target field in the AWT event
long whenTime of event creation.
String typeNull for AWT events
String targetNameNull for AWT events
boolean isAWTTrue for AWT events
public boolean handleMessage(Message msg); public boolean handleEvent(Message msg, Event evt); public boolean action(Message msg, Event evt, Object what); public boolean mouseDown(Message msg, Event evt, int x, int y); public boolean mouseDrag(Message msg, Event evt, int x, int y); public boolean mouseUp(Message msg, Event evt, int x, int y); public boolean mouseMove(Message msg, Event evt, int x, int y); public boolean mouseEnter(Message msg, Event evt, int x, int y); public boolean mouseExit(Message msg, Event evt, int x, int y); public boolean keyDown(Message msg, Event evt, int key); public boolean keyUp(Message msg, Event evt, int key); public boolean gotFocus(Message msg, Event evt, Object what); public boolean lostFocus(Message msg, Event evt, Object what);
handleMessage is invoked for all messages, whether the message is from a subgroup or is the result of an AWT event. The implementation of handleMessage in the Group class invokes handleEvent for all AWT events.
Code excerpt from "Group.java":
public boolean handleMessage(Message msg) { if (msg.isAWT) return handleEvent(msg, (Event)msg.arg); else return true; }
handleEvent is only invoked for messages that come from AWT events. The implementation of handleEvent in the Group class invokes other methods depending on the type of the event. For example, MOUSE_DOWN events cause the mouseDown method to be invoked. By overridding the mouseDown method, all MOUSE_DOWN events originating inside the group will be caught.
Code excerpt from Group.java:
public boolean handleEvent(Message msg, Event evt) { switch (evt.id) { case Event.MOUSE_ENTER: return mouseEnter(msg, evt, evt.x, evt.y); case Event.MOUSE_EXIT: return mouseExit(msg, evt, evt.x, evt.y); case Event.MOUSE_MOVE: return mouseMove(msg, evt, evt.x, evt.y); case Event.MOUSE_DOWN: return mouseDown(msg, evt, evt.x, evt.y); case Event.MOUSE_DRAG: return mouseDrag(msg, evt, evt.x, evt.y); case Event.MOUSE_UP: return mouseUp(msg, evt, evt.x, evt.y); case Event.KEY_PRESS: case Event.KEY_ACTION: return keyDown(msg, evt, evt.key); case Event.KEY_RELEASE: case Event.KEY_ACTION_RELEASE: return keyUp(msg, evt, evt.key); case Event.ACTION_EVENT: return action(msg, evt, evt.arg); case Event.GOT_FOCUS: return gotFocus(msg, evt, evt.arg); case Event.LOST_FOCUS: return lostFocus(msg, evt, evt.arg); default: return false; } }
The next example demonstrates action event handling. Action events are generated by AWT components and translated by handleEvent() into calls on the action method. The example assumes the creation of a GUI description containing a button named "OK" and a button named "Cancel." If an action event originates from the "OK" button or the "Cancel" button, a message is printed.
The AWT button component generates an action event when it is
clicked.
public boolean action(Message msg, Event evt, Object what) { boolean handled = false; if (msg.target == gui.ok) { System.out.println("OK"); handled = true; } else if (msg.target == gui.cancel) { System.out.println("Cancel"); handled = true; } // All message handling methods should be true if the message // was handled, and false otherwise. // // The return value determines if the message should be passed // on to the operation classes, and subsequently on up the // group tree. AWT messages are never propagated up the group // tree regardless of the return value, but they are forwarded // to the operations classes depending on the return value. // [ operation classes are currently not implemented ] return handled; }
See also: