Last updated April 14, 2010 02:30, by Eric Heumann
Feedicon  

How To: @Action Annotation

The Swing Application Framework provides a simple way of creating Swing Actions for your Application. If you'd like to learn more about using the Action interface and its most common implementation, the AbstractAction class, check out the Swing tutorial article on them.

Actions created using the @Action annotation in SAF will be instances of ApplicationAction. These are created automatically by the ApplicationActionMap for the class to which the Action belongs, one Action for each method that has the @Action annotation. ApplicationAction objects are aware of the Application, ApplicationContext, and ApplicationActionMap that they reside in. They thus have access to the ResourceMap of the class in which they are declared, and this ResourceMap is used to load the name, descriptions, icons, accelerators, and mnemonics of the Action from the class's properties file.

The @Action annotation and ApplicationAction thus eliminate the work that would otherwise be required to instantiate an Action, load and inject resources into it, and add it to an ActionMap of a component. All that's left up to the developer is using them (e.g. installing them into a button).


Here is how to...

Create

First you need to create a method that will handle the ActionEvents of the Action. This method is equivalent to the actionPerformed method that must be overridden in AbstractAction implementations. Therefore, whatever code you'd normally put in an actionPerformed method, place in the method you've just created.

Note that ApplicationAction instances generated using this method will be associated with instances of the class in which you write the method, so be sure to put the method in the appropriate class. Also, the name of the method will, by default, be the name/key of the ApplicationAction which is used to declare resources for the Action and is used to get an instance of the Action from ActionMaps.

Next, place the @Action annotation on the method.

Example

@Action
public void sayHello() {
    System.out.println("Hola Mundo");
}

Acquire an Instance

Assuming that you've created a method for the Action and placed the @Action annotation on it, an instance of ApplicationAction will be lazily created during run time and placed in the ActionMap for each instance of the class in which the @Action method is defined. This ActionMap can be accessed through the ApplicationContext of the Application.

Once the ActionMap is acquired, the Action can be retrieved using the map's get method and the Action's key (the name of the @Action method by default).

Example

public class MyClass {
    
    private JButton button;
    
    public MyClass() {
        Action sayHelloAction =
                Application.getContext().getActionMap(this).get("sayHello");
        button = new JButton(sayHelloAction);
    }
    
    @Action
    public void sayHello() {
        System.out.println("Hola Mundo");
    }
    
}

Inject Resources

Resource injection is handled automatically when the Application creates the Action. All you have to do is create a resource properties file for the class in which the @Action method is defined and then create entries for the various resources you'd like to inject.

Resources for Actions must be in the following format: name.Action.property = value where name is the key or name of Action and property is the property to inject. For a full list of which properties are recognized, see the @Action reference wiki.

Note that, as with all resource files for classes using SAF, the resource file should be placed in folder called "resources". So if a class's full name is org.bsaf.wiki.acthowto.MyClass, then resource values should be entered into the file org/bsaf/wiki/acthowto/resources/MyClass.properties.

Example

sayHello.Action.text = _Say Hello
sayHello.Action.icon = hello.png
sayHello.Action.accelerator = control shift S
sayHello.Action.shortDescription = Prints "hello world" in Spanish

Bind State to Property

The two boolean states of an ApplicationAction, enabled and selected, can be bound to a bean property of the class in which it is created. Whenever that property of the class to which it is bound changes (and fires a PropertyChangeEvent), the state of the Action will change to reflect the new property value.

Note that the selected Action property will only affect Swing buttons that use this property, such as JCheckBox, JCheckBoxMenuItem, JRadioButton, JRadioMenuButton, and JToggleButton. The enabled Action property will affect all buttons.

Example

In this example, whenever the MyClass property vocal is false, as is the default here, the sayHello Action will be disabled. When setVocal(true) is called, the Action will become enabled.

public class MyClass {
    
    private boolean vocal;
    
    public MyClass() {
        setVocal(false);
    }
    
    @Action(enabledProperty = "vocal")
    public void sayHello() {
        System.out.println("Hola Mundo");
    }
    
    public boolean isVocal() {
        return vocal;
    }
    
    public void setVocal(boolean vocal) {
        boolean old = this.vocal;
        this.vocal = vocal;
        firePropertyChange("vocal", old, vocal)
    }
    
}




 Under Construction
 More How-To Cases to Come!
 EMH 4/13/10
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2013, Oracle Corporation and/or its affiliates
(revision 20131025.e7cbc9d)
 
 
Close
loading
Please Confirm
Close