
One shortcoming of working with actions is that there is no way to tell a JMenuBar
or JToolBar to display just text or just icons for actions. Although you might like
an action’s name to be displayed in a menu and its icon to be displayed in a tool-
bar, both
JMenuBar and JToolBar display an action’s textual name and its icon.
The
Action inter face helps you implement a clean separation between GUI code
and application logic. Remember, however, that you cannot just instantiate
Action
objects directly. Since Action is a kind of ActionListener, you must define an
individual subclass of
Action that implements the actionPerformed() method for
each of your desired actions. The
AbstractAction class is helpful here; it imple-
ments everything except the
actionPerformed() method.
Tooltips
A Swing component can display context-sensitive help to the user in the form of a
tooltip:asmall window that pops up when the user lets the mouse rest over the
component. You can display text in this window that explains the purpose or
function of the component. Specify this text with the
setToolTipText() method.
This
toolTipText pr operty is inherited from JComponent,soitisshar ed by all
Swing components.
While it is a good idea to provide tooltips for the benefit of your novice users,
your experienced users may find them annoying, so it is nice to provide a way to
tur n them off. You can do this programatically by setting the