Java Reference
In-Depth Information
Toolbars are rows or columns of components that group the most commonly used pro-
gram options together. Toolbars often contain buttons and lists and can be used as an
alternative to using pull-down menus or shortcut keys.
Toolbars are horizontal by default, but the orientation can be explicitly set with the
HORIZONTAL or VERTICAL class variables of the SwingConstants interface.
Constructor methods include the following:
JToolBar() —Creates a new toolbar
n
JToolBar( int ) —Creates a new toolbar with the specified orientation
n
After you have created a toolbar, you can add components to it by using the toolbar's
add( Object ) method, where Object represents the component to place on the toolbar.
Many programs that use toolbars enable the user to move the bars. These are called dock-
able toolbars because you can dock them along an edge of the screen, similar to docking
a boat to a pier. Swing toolbars also can be docked into a new window, separate from the
original.
10
For best results, a dockable JToolBar component should be arranged in a container using
the BorderLayout manager. A border layout divides a container into five areas: north,
south, east, west, and center. Each of the directional components takes up whatever space
it needs, and the rest are allocated to the center.
The toolbar should be placed in one of the directional areas of the border layout. The
only other area of the layout that can be filled is the center. (You'll learn more about lay-
out managers such as border layout during tomorrow's lesson, Day 11, “Arranging
Components on a User Interface.”)
Figure 10.12 shows a dockable toolbar occupying the north area of a border layout. A
text area has been placed in the center.
FIGURE 10.12
A dockable toolbar
and a text area.
Search WWH ::




Custom Search