ptg7068951
228 HOUR 16:Building a Complex User Interface
Image icons often are used in toolbars, containers that group several com-
ponents together into a row or column.
Toolbars, which are created by using the JToolBarclass, can be designed so
that a user can move them from one part of a GUI to another. This process
is called docking, and these components are also called dockable toolbars.
You can create a toolbar with one of the following constructor methods:
. JToolBar()—Create a toolbar that lines up components in a horizon-
tal direction
. JToolBar(int)—Create a toolbar that lines up components in the
specified direction, which is either SwingConstants.HORIZONTALor
SwingConstants.VERTICAL.
Components are added to a toolbar in the same way they are added to
other containers—the add(Component)method is called with the compo-
nent to be added.
For a toolbar to be dockable, it must be placed in a container that uses
BorderLayoutas its layout manager. This layout arranges a container into
north, south, east, west, and center areas. When you are using a dockable
toolbar, however, the container only should use two of these: the center
and one directional area.
The toolbar should be added to the directional area. The following state-
ments create a vertical, dockable toolbar with three icon buttons:
BorderLayout border = new BorderLayout();
pane.setLayout(border);
JToolBar bar = new JToolBar(SwingConstants.VERTICAL);
ImageIcon play = new ImageIcon(“play.gif”);
JButton playButton = new JButton(play);
ImageIcon stop = new ImageIcon(“stop.gif”);
JButton stopButton = new JButton(stop);
ImageIcon pause = new ImageIcon(“pause.gif”);
JButton pauseButton = new JButton(pause);
bar.add(playButton);
bar.add(stopButton);
bar.add(pauseButton);
add(bar, BorderLayout.WEST);
The next project you undertake during this hour is Tool, a Java application
that includes image icons and a dockable toolbar around. Create an empty
Java file called Tool, enter Listing 16.3 in the file, and save the file.