Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components

How to Use Tool Bars

A JToolBar(in the API reference documentation) is a container that groups several components -- usually buttons with icons -- into a row or column. Often, tool bars provide easy access to functionality that is also in menus. If this is the case, then in addition to this section, you should read How to Use Actions.

The following pictures show an application that contains a tool bar above a text area.

By default, the user can drag the tool bar to a different edge of its container or out into a window of its own. The next figure shows how the application looks after the user has dragged the tool bar to the right edge of its container. To make the drag-out behavior work correctly, the tool bar should be in a container that uses BorderLayout, and the container should have only one other component, which should be in the center.
The next figure shows how the application looks after the user has dragged the tool bar outside its window.

The following code implements the tool bar. You can find the entire program in ToolBarDemo.java. It relies on these images: left.gif, middle.gif, and right.gif.


Note: If any buttons in your tool bar duplicate functionality of other components, such as menu items, then you should probably create and add the tool-bar buttons as described in How to Use Actions.
public ToolBarDemo() {
    ...
    JToolBar toolBar = new JToolBar();
    addButtons(toolBar);
    ...
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    ...
    contentPane.add(toolBar, BorderLayout.NORTH);
    contentPane.add(scrollPane, BorderLayout.CENTER);
    ...
}

protected void addButtons(JToolBar toolBar) {
    JButton button = null;

    //first button
    button = new JButton(new ImageIcon("images/left.gif"));
    ...
    toolBar.add(button);

    //second button
    button = new JButton(new ImageIcon("images/middle.gif"));
    ...
    toolBar.add(button);

    //third button
    button = new JButton(new ImageIcon("images/right.gif"));
    ...
    toolBar.add(button);
}

By adding a few lines of code to the preceding example, we can demonstrate some more tool bar features:

Here is a picture of the new UI, which is implemented in ToolBarDemo2.java:
Because the tool bar can no longer be dragged, it no longer has bumps at its left edge. Here's the code that turns off dragging:
toolBar.setFloatable(false);
The biggest visible difference is that the tool bar contains two new components, which are preceded by a blank space -- a separator. Here is the code that adds the separator:
toolBar.addSeparator();
Here is the code that adds the new components:
...//add to where the first button is initialized:
button.setAlignmentY(CENTER_ALIGNMENT);
...//add to where the second button is initialized:
button.setAlignmentY(CENTER_ALIGNMENT);
...//add to where the third button is initialized:
button.setAlignmentY(CENTER_ALIGNMENT);
...
//fourth button
button = new JButton("Another button");
...
button.setAlignmentY(CENTER_ALIGNMENT);
toolBar.add(button);

//fifth component is NOT a button!
JTextField textField = new JTextField("A text field");
...
textField.setAlignmentY(CENTER_ALIGNMENT);
toolBar.add(textField);
The setAlignmentY calls are necessary to make the tool bar's components align nicely. If the code doesn't set the alignment, then the text field is positioned too high. This is the result of JToolBar using BoxLayout as its layout manager, and of buttons and text fields having different default Y alignments. If you encounter layout problems in a tool bar, see How to Use BoxLayout for help.

The Tool Bar API

The following table lists the commonly used JToolBar(in the API reference documentation)constructors and methods. Other methods you might call are defined by the JComponent(in the API reference documentation), Container(in the API reference documentation), and Component(in the API reference documentation) classes.

Method Purpose
JToolBar() Create a tool bar.
JButton add(Action)
Component add(Component) void addSeparator()
Add a component (usually a button) to the tool bar. If the argument to add is an Action object, then the tool bar automatically creates a JButton and adds it.
void addSeparator() Add a separator to the end of the tool bar.
void setFloatable(boolean)
boolean isFloatable()
The floatable property is true by default, to indicate that the user can drag the tool bar out into a separate window. To turn off tool bar dragging, use toolbar.setFloatable(false).

Examples that Use Tool Bars

This table lists examples that use JToolBar and where those examples are described.

Example Where Described Notes
ToolBarDemo.java This page. A basic tool bar with icon-only buttons.
ToolBarDemo2.java This page Demonstrates a non-floatable tool bar containing a separator and non-button components.
ActionDemo.java How to Use Actions Implements a tool bar using Action objects.


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components