Java Reference
In-Depth Information
use this to create buttons for any of the
Action
objects that you created for the menus, and have the toolbar
button events taken care of without any further work.
For example, you could add a button you create from the
openAction
object corresponding to the
Open
menu item in the File menu with the following statements:
JButton button = new JButton(openAction); // Create button from Action
toolBar.add(button); // Add a toolbar button
That's all you need basically. The
JButton
constructor creates a
JButton
object based on the
Action
object that you pass as the argument. The properties defined in the
Action
object apply to the button, so this
matches the Open menu item in the File menu. More than that, because the button and the menu item share
a single
Action
object, clicking on either the menu item or the toolbar button calls the same
actionPer-
formed()
method, which is the one you have defined for the
Action
object. The
add()
method for
toolBar
adds the
JButton
object to the toolbar.
A nice feature of a
JToolBar
object is that it can highlight the button that the mouse cursor is over with
a mechanism that is referred to as
rollover action
. To switch on this feature you can call
setRollover()
for
the
JToolBar
object with the argument as
true
:
toolBar.setRollover(true);
Add this statement to the
SketcherFrame
constructor, immediately before you add the toolbar to the con-
tent pane. Calling the
setRollover()
method with
false
as the argument switches off rollover action. You
will see button highlighting in operation in Sketcher.
Inhibiting Text on a Toolbar Button
Because a toolbar button is created using the
Action
object properties that you have defined for the corres-
ponding menu item, it has a
NAME
value, and the text for the name displays on the toolbar button by default.
This is not want you want in most cases. You can prevent the name from being displayed by calling the
setHideActionText()
for a
JButton
toolbar button with an argument value
true
:
button.setHideActionText(true);
Calling this method with a
false
argument would cause the text to be displayed, so you can switch the text
on and off. What you really want, though, is to be able to display an icon on a toolbar button instead of
displaying the text.
Adding Icons to Toolbar Buttons
A reference to an icon is generally stored in a variable of type
javax.swing.Icon
, which is an interface
that declares methods that return the height and width of an icon in pixels —
getHeight()
and
getWidth()
methods, respectively. The
Icon
interface also declares the
paint()
method that paints the icon image on
a component. The
javax.swing.ImageIcon
class implements the
Icon
interface and you use this class to
create an icon object in your program from a file that contains the data defining the icon image.
The
ImageIcon
class has several constructors, and the one you use accepts a
String
argument that spe-
cifies the path for the file in which the icon image is found. The
String
object that you pass as the argument
can be just the file name, in which case the file should be in the current directory when Sketcher executes.
You can also supply a string that specifies the full path to the file containing the image. I'm putting the files