Using Groups and Shadows (Advanced)

This is a continuation of the discussion started in Using Groups and Shadows (Basic)

Shadows

Shadows are wrapper classes for AWT components. Shadows do several things for you:

Custom functionality may also be included in the shadow classes. For example, the window shadow will resize the window to its preferred size and map the window when its visible attribute is set to true.

Rules for writing a shadow:

Groups

A group encapsulates a set of shadows and subgroups. Visual Java is used to create new groups out of existing shadows and groups. When a new group is created, it can be imported into the Visual Java palette and can then be incorporated into other groups. New groups must be subclasses of the Group class.

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.

From an external viewpoint, a group is a single component that has a set of attributes, public methods, and messages:

Attributes
Attributes for a group are defined in the constructor and are handled using the getOnGroup and setOnGroup methods. Support is provided for forwarding a set of attributes to one or more children of the group--a child being either a shadow or a subgroup.

Methods
Most methods are custom methods written by the author of the group. There are some useful methods defined in the Group class itself, such as the get/set attribute methods.

Messages
A group will create messages and post them to their parent groups. By posting messages, a group does not need to have any knowledge of its parent. Instead, the parent can listen for a particular message from its subgroup and perform an action based on the message.

Rules for Writing a Group

How to Put AWT Components That Don't Have a Shadow Inside Your Group

Just add the AWT component as a child of an existing panel or frame that is already inside the group. The events from the AWT component will be delivered to the group. The only difference is that the message target will be the same as the AWT event target. Normally the message target is set to the shadow corresponding to the AWT event target, but in this case there is no corresponding shadow.

How to Use Groups Without the Generated Main

The following examples show how to use groups without the generated main.

Main

  // Construct the group

  Group group = new MyFancyGroup();

  // Set the group environment.  The "args" are command 
  // line arguments.

  group.setEnvironmentInfo(null, args);

  // Initialize the group

  group.initialize();

  // Set the top level on the group

  WindowShadow win = (WindowShadow)group.getWindow();
  if (win instanceof FrameShadow) {
    win.createBody();
    group.setTopLevel((Frame)win.getBody());
  }
  else {
    group.setTopLevel(/* Pass in an existing frame window
			 from your application */);
  }

  // Create the group

  group.create();

  // If the group is a panel group, add it in the 
  // hierarchy somewhere

  if (group.getPanel() != null) {
    myApplicationFrame.add((Panel)group.getPanel().getBody());
  }

  // Start the group

  group.start();

Applet

  // Construct the group

  Group group = new MyFancyGroup();

  // Set the group environment.  The "applet" must be available here.

  group.setEnvironmentInfo(myApplet, null);

  // Initialize the group

  group.initialize();

  // Set the top level on the group

  WindowShadow win = (WindowShadow)group.getWindow();
  if (win instanceof FrameShadow) {
    win.createBody();
    group.setTopLevel((Frame)win.getBody());
  }
  else {
    // Figure out the applet's frame

    Component comp = applet;
    while (comp != null && !(comp instanceof Frame))
      comp = comp.getParent();
    group.setTopLevel((Frame)comp);
  }

  // Create the group

  group.create();

  // If the group is a panel group, add it in the 
  // hierarchy somewhere

  if (group.getPanel() != null) {
    myApplet.add((Panel)group.getPanel().getBody());
  }

The start, stop, and destroy methods should be forwarded from the applet to the group:

  group.start();
  group.stop();
  group.destroy();


See also:

Using Groups and Shadows (Basic)
Visual Java Overview
The Visual Menu
Visual Java Components
Laying Out GUI Interfaces
Generating Java Source Code
Adding Operations (Filters and Actions)
Visual Java Runtime Classes
Creating Menus
Adding Custom Components and Windows
Visual Java API Documentation
Visual Java Runtime Packages
Class Hierarchy
Index of all Fields and Methods