Java Reference
In-Depth Information
key for each of the actions except for those corresponding to the Close and Exit menu items. The icons
are 24 ×24 pixels, a standard size for use on the toolbar. You could use icons of any size and the toolbar
adjusts in height to accommodate them. Of course, if the icons are very big, this limits how many you
can have in a single row.
The addToolBarButton() method creates a single button and adds it to the toolbar. It creates the JBut-
ton object from the Action object that is passed as the argument. The setBorder() method adds the
border specified by the argument to a button. You add a compound border that you create by calling
the createCompoundBorder() method from the BorderFactory class. The first argument is the outside
border. This is an empty border so it determines the space between buttons on the toolbar and between
the buttons and the top and bottom of the toolbar. The integer arguments to the EmptyBorder class con-
structor correspond to the widths of the bottom, left, right, and top edges of the border.
The createToolBar() method that you added calls the addToolBarButton() method for each of the
actions for which you want a toolbar button. Calling this method in the SketcherFrame constructor adds
the buttons to the toolbar.
A feature that comes for free with a JToolBar object is that it is automatically dockable and can float as
an independent window. You can drag the toolbar using the mouse by holding mouse button 1 down with
the cursor in the gray area to the left of the button and then dragging it to a new position. The toolbar
turns into a free-floating window, as Figure 18-10 shows.
FIGURE 18-10
You can also drag the toolbar to any of the four edges of the content pane, or to its original docked posi-
tion again at the top of the content pane. You must drag with the cursor in the gray area of the toolbar to
redock it. Dragging with the cursor in the toolbar title area just moves the window.
It's not always convenient to have the toolbar floating. You can inhibit the capability to drag the toolbar
around by calling the setFloatable() method for the JToolBar object with the argument specified as
false . If you want to try this out in Sketcher, add the following statement to the SketcherFrame con-
structor before the statement that adds the toolbar to the content pane:
toolBar.setFloatable(false);
// Inhibit toolbar
floating
getContentPane().add(toolBar, BorderLayout.NORTH);
}
 
 
Search WWH ::




Custom Search