Java Reference
In-Depth Information
// Listeners
public void addPropertyChangeListener(PropertyChangeListener listener);
public void removePropertyChangeListener(PropertyChangeListener listener);
// Properties
public boolean isEnabled();
public void setEnabled(boolean newValue);
// Other methods
public Object getValue(String key);
public void putValue(String key, Object value);
}
Because Action is merely an interface, the Swing libraries offer a class to implement the
interface. That class is AbstractAction .
AbstractAction Class
The AbstractAction class provides a default implementation of the Action interface. This is
where the bound property behavior is implemented.
Using Actions
Once you define an AbstractAction by subclassing and providing a public void actionPerformed
(ActionEvent actionEvent) method, you can then pass it along to some special Swing compo-
nents. JButton , JCheckBox , JRadioButton , JToggleButton , JMenuItem , JCheckBoxMenuItem , and
JRadioButtonMenuItem provide constructors for creating the components from actions, whereas
the Swing text components have their own built-in support for Action objects through their
Keymap , InputMap , and ActionMap .
When the component with the associated Action is added to the respective Swing container,
selection triggers the calling of the actionPerformed(ActionEvent actionEvent) method of the
Action . The display of the component is defined by the property elements added to the internal
data structure. To demonstrate, Listing 2-8 presents an Action with a “Print” label and an
image icon. When this is activated, a “Hello, World” message is printed.
Listing 2-8. Action Usage Example
import java.awt.event.*;
import javax.swing.*;
public class PrintHelloAction extends AbstractAction {
private static final Icon printIcon = new ImageIcon("Print.gif");
PrintHelloAction() {
super("Print", printIcon);
putValue(Action.SHORT_DESCRIPTION, "Hello, World");
}
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Hello, World");
}
}
Search WWH ::




Custom Search