Java Reference
In-Depth Information
Example 10•17: CommandAction.java (continued)
/**
* Create an Action object that has the various specified attributes,
* and invokes the specified Command object in response to ActionEvents
**/
public CommandAction(Command command, String label,
Icon icon, String tooltip,
KeyStroke accelerator, int mnemonic,
boolean enabled)
{
this.command = command; // Remember the command to invoke
// Set the various action attributes with putValue()
if (label != null) putValue(NAME, label);
if (icon != null) putValue(SMALL_ICON, icon);
if (tooltip != null) putValue(SHORT_DESCRIPTION, tooltip);
if (accelerator != null) putValue(ACCELERATOR_KEY, accelerator);
if (mnemonic != KeyEvent.VK_UNDEFINED)
putValue(MNEMONIC_KEY, new Integer(mnemonic));
// Tell the action whether it is currently enabled or not
setEnabled(enabled);
}
/**
* This method implements ActionListener, which is a super-interface of
* Action. When a component generates an ActionEvent, it is passed to
* this method. This method simply passes it on to the Command object
* which is also an ActionListener object
**/
public void actionPerformed(ActionEvent e) { command.actionPerformed(e); }
// These constants are defined by Action in Java 1.3.
// For compatibility with Java 1.2, we re-define them here.
public static final String ACCELERATOR_KEY = "AcceleratorKey";
public static final String MNEMONIC_KEY = "MnemonicKey";
}
Custom Dialogs
The Scribble program of Example 10-16 displayed two kinds of dialogs: a confir-
mation dialog created by JOptionPane and a color selection dialog created by
JColorChooser . These Swing components support many basic dialog box needs.
The JOptionPane class makes it easy to display simple (and not-so-simple) infor-
mation, confirmation, and selection dialogs, while JColorChooser and JFile-
Chooser provide color and file selection capabilities. Most nontrivial applications,
however, need to create custom dialogs that go beyond these standard compo-
nents. This is easy to do with the JDialog component.
Example 10-18 shows the FontChooser class. It subclasses JDialog and uses the
ItemChooser class developed in Example 10-15 to display font families, styles, and
sizes to the user. A FontChooser dialog is pictured in Figure 10-14. The inner class
FontChooser.Demo is a simple demonstration application you can use to experi-
ment with the FontChooser dialog.
Search WWH ::




Custom Search