19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

«interface»<br />

java.awt.event.ActionListener<br />

«interface»<br />

javax.swing.Action<br />

+getValue(key: String): Object<br />

+isEnabled(): boolean<br />

+putValue(key: String, value: Object): void<br />

+setEnabled(b: boolean): void<br />

Gets one of this object's properties using the associated key.<br />

Returns true if action is enabled.<br />

Associates a key/value pair with the action.<br />

Enables or disables the action.<br />

javax.swing.AbstractAction<br />

+AbstractAction()<br />

+AbstractAction(name: String)<br />

+AbstractAction(name: String, icon: Icon)<br />

+getKeys(): Object[]<br />

Defines an Action object with a default description string and<br />

default icon.<br />

Defines an Action object with the specified description string<br />

and a default icon.<br />

Defines an Action object with the specified description string<br />

and the specified icon.<br />

Returns an array of objects which are keys for which values<br />

have been set for this AbstractAction, or null if no keys<br />

have values set.<br />

Figure 38.8<br />

The Action interface provides a useful extension <strong>to</strong> the<br />

ActionListener interface in cases where the same functionality<br />

may be accessed by several controls. The AbstractAction class<br />

provides a default implementation for Action.<br />

AbstractAction is a default implementation of the Action<br />

interface, as shown in Figure 38.8. It implements all the methods<br />

in the Action interface except the actionPerformed method.<br />

Additionally, it defines the getKeys() method.<br />

Since AbstractAction is an abstract class, you cannot create an<br />

instance using its construc<strong>to</strong>r. However, you can create a<br />

concrete subclass of AbstractAction and implement the<br />

actionPerformed method. This subclass can be conveniently defined<br />

as an anonymous inner class. For example, the following code<br />

creates an Action object for terminating a program.<br />

Action exitAction = new AbstractAction("Exit") {<br />

public void actionPerformed(ActionEvent e) {<br />

System.exit(1);<br />

}<br />

};<br />

Certain containers, such as JMenu and JToolBar, know how <strong>to</strong> add<br />

an Action object. When an Action object is added <strong>to</strong> such a<br />

container, the container au<strong>to</strong>matically creates an appropriate<br />

<strong>com</strong>ponent for the Action object and registers a listener with the<br />

Action object. Here is an example of adding an Action object <strong>to</strong> a<br />

menu and a <strong>to</strong>olbar:<br />

jMenu.add(exitAction);<br />

jToolBar.add(exitAction);<br />

Several Swing <strong>com</strong>ponents, such as JBut<strong>to</strong>n, JRadioBut<strong>to</strong>n, and<br />

JCheckBox, contain construc<strong>to</strong>rs <strong>to</strong> create instances from Action<br />

objects. For example, you can create a JBut<strong>to</strong>n from an Action<br />

object, as follows:<br />

15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!