Programmatic Configuration

Most of the time GUI Commands will automatically configure your commands based on their id. Occassionally you need to create commands programmatically without any configuration. In this case you can use anonymous commands.

Anonymous Commands

Anonymous commands are the same as normal commands except that they are created without an id. This is achieved by using the no-arg constructor.

Anonymous commands do have two restrictions however, they are as follows:

  1. They must be configured programmatically before creating buttons and menu items.
  2. They can't be automatically included in groups.

The following shows an example of creating an anonymous command and configuring its default face.

   // create an anonymous command using the no-arg constructor
   ActionCommand myCommand = new ActionCommand()
   {
      public void handleExecute()
      {
         Toolkit.getDefaultToolkit().beep();
      }
   }

   // and programmatically configure its face
   myCommand.getDefaultFace(true).setText("My Command");

This command can only be added to a group using either a Group Builders or Expansion Points. See Using Faces Programmatically for more information on creating faces.

Creating Commands with no Configuration

In cases where you need an Id but don't want to specify or load any configuration you can override the loadConfiguration() method an leave it empty. If you don't override the method, an exception will be thrown during construction. As with anonymous commands you will need to create faces programmatically before create buttons or menu items.

The following example shows a command with the id my-command that will not attempt to load any configuration:

   // create a command with an id but no configuration.
   ActionCommand noConfig = new ActionCommand("my-command")
   {
      @Override
      protected void loadConfiguration()
      {
         // don't try and load any configuration.
      }

      public void handleExecute()
      {
         Toolkit.getDefaultToolkit().beep();
      }
   }

   // programmatically configure its face
   noConfig.getDefaultFace(true).setText("My Command");