Actions Editor

# Action Editor

The action part of an operation determines what happens when there is a matching event. GUI builder specifically supports some common actions, such as showing and hiding a component, exiting the application, and setting an attribute on a component. You can also specify custom code that is executed when the action is invoked.

Common actions and custom code actions are not treated in quite the same way. Common actions are live both while the application is running and while you are building the application. Custom code actions are live only while the program is running. Future versions of GUI builder might support live code actions while building the application.

The exit action is not live while building the application because it would cause GUI builder to exit and unsaved changes would be lost.

## Action Types

Choose one of the following action types from the Action Type menu:

- Show
- Hide
- Exit
- Set Attribute
- Execute Code

### Show, Hide

After you choose one of these actions from the menu, you must select a target for the action from the Target display pane. The target is the component that is shown or hidden when the action is invoked.

### Exit

There is no target for the exit action.

### Set Attribute

After you choose Set Attribute from the menu, you must:

1. Select a component from the Target pane and an attribute of that component from the Name pane.
2. Choose either Constant or Event Arg from the Value Source menu.

   If you choose the Constant from the menu, you can statically set the value using the provided windows, text boxes, and so on.

   If you choose Event Arg from the menu, the event argument must be the same type as the attribute that you have selected. For example, checkboxes have Boolean event arguments. The `enabled` attribute defined for components is also a Boolean value. Therefore, you can legally choose the `event arg` option for the checkbox as long as you select the `enabled` attribute for the target component. Defining an action in this way causes the checkbox to toggle the component between the enabled and disabled states.

## Execute Code

If you choose the Execute Code item in the Action Type menu, a text box is available for you to enter custom code. You can create custom callbacks that are automatically included in your application.

The code you enter in the Execute Code box is saved in the `.gui` file, and is later generated into the project-name`Ops.java` file.

### Special Variables

There are several important variables that are available within the scope of the custom code segment.

For a group named `MyProg`:

- `MyProg group;`
  Group variable. The group is guaranteed to be initialized when the action is invoked.

- `MyProgRoot gui;`
  Root variable. The root will be non-null when the action is invoked.

- `Message msg;`
  The message that triggered the action. The message will always be non-null.

- `Event evt;`
  AWT event that triggered the action. This will be null only if the triggering message is not an AWT event. This can happen if the operation is using a message filter instead of an event filter.

The following is a code segment that shows the window `frame1` when a button is clicked:

      gui.frame1.set("visible", Boolean.true);

The following is a code segment that shows or hides the window `frame1` depending on the state of a checkbox:

      gui.frame1.set("visible", evt.arg);

### Import Statements

You can add import statements at the top of your custom code. The code generator will place them in the correct location in the generated sources. For example, if you type:

      import java.net.*;
      URL url = new URL(gui.urlTF.get("text"));
      URLConnection connection = url.openConnection();

the following code is generated into the project-name`Ops.java` file. Note the position of the import statement.

      import java.io.net.*;
      private void handleCallback(int index, Message msg, Event evt) {
        switch (index) {
        case 1:
          {
    group.exit();
          }
          break;
        case 2:
          {
    URL url = new URL(gui.urlTF.get("text"));
    URLConnection connection = url.openConnection();
          }
         }
      }

### Accessing AWT Methods for Shadows Classes

The `getBody()` call allows you to access methods of the classes on which shadow classes are based. For example, if you want to determine which item is selected in a `ListShadow` object, type:

      String string_selected=((List) listShadow.getBody()).getSelectedItem();

In general, it is better to use the shadow class for the `get()` and `set()` methods rather than the `getBody()` method which accesses the AWT component directly. The shadow class methods utilize workarounds and optimizations. For example:

    String string_selected=listShadow.get("selectedItem");

### Accessing the Applet

The `group` class provides a method for accessing the applet. For example, if in your operation you need to access a parameter supplied to the applet (in this case the blink rate), type:

     Applet ap = getApplet();
     String param = ap.getParameter("blinkrate");

See also:

  What Is a Component Operation?
  Adding Operations to Components
  GUI Builder Runtime Classes
  What Are Groups and Shadows?
  More on Groups and Shadows

  Visual Java GUI Builder API Documentation
    Visual Java GUI Builder Runtime Packages
    Class Hierarchy
    Index of All Fields and Methods