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.

The <strong>to</strong>olbar but<strong>to</strong>ns are floatable.<br />

You can also set a title for the floatable <strong>to</strong>olbar, as shown in<br />

Figure 38.7(a). To do so, create a <strong>to</strong>olbar using the<br />

JToolBar(String title) construc<strong>to</strong>r. If you set floatable false,<br />

the floatable controller is not displayed, as shown in Figure<br />

38.7(b). If you set a border (e.g., a line border), as shown in<br />

Figure 38.7(c), the line border is displayed and the floatable<br />

controller is not displayed.<br />

(a) (b) (c)<br />

Figure 38.7<br />

The <strong>to</strong>olbar but<strong>to</strong>ns can be cus<strong>to</strong>mized in many forms.<br />

TIP: For the floatable feature <strong>to</strong> work<br />

properly, do the following: (1) place a <strong>to</strong>olbar<br />

<strong>to</strong> one side of the container of BorderLayout<br />

and add no <strong>com</strong>ponents <strong>to</strong> the other sides; (2)<br />

don’t set border on a <strong>to</strong>olbar. Setting a border<br />

would make it non-floatable.<br />

38.5 Processing Actions Using the Action Interface<br />

Often menus and <strong>to</strong>olbars contain some <strong>com</strong>mon actions. For<br />

example, you can save a file by choosing File, Save, or by<br />

clicking the save but<strong>to</strong>n in the <strong>to</strong>olbar. Swing provides the<br />

Action interface, which can be used <strong>to</strong> create action objects for<br />

processing actions. Using Action objects, <strong>com</strong>mon action<br />

processing can be centralized and separated from the other<br />

application code.<br />

The Action interface is a subinterface of ActionListener, as<br />

shown in Figure 38.8. Additionally, it defines several methods<br />

for checking whether the action is enabled, for enabling and<br />

disabling the action, and for retrieving and setting the<br />

associated action value using a key. The key can be any string,<br />

but four keys have predefined meanings:<br />

Key<br />

Action.NAME<br />

Action.SMALL_ICON<br />

Action.SHORT_DESCRIPTION<br />

Action.LONG_DESCRIPTION<br />

Description<br />

A name for the action<br />

A small icon for the action<br />

A <strong>to</strong>ol tip for the action<br />

A description for online help<br />

14

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

Saved successfully!

Ooh no, something went wrong!