| JToolBar |
|---|
JToolBar. In the most basic use it is little more
than a JPanel, acting as a container to hold small buttons. However,
the main distinction is that JToolBar is dockable (or floatable),
meaning that it can be dragged out of the original window and kept as a
standalone window. It can also be dragged back into the window, or dropped
into the side of the window even if it was originally placed at the top.
To build a JToolBar, you simply call the empty constructor
(for a horizontal toolbar), or pass in JToolBar.VERTICAL.
You typically place a horizontal toolbar in the north or south region of a
container that uses BorderLayout, and a vertical toolbar in the east
or west region. What little complexity there is with JToolBar comes
from the buttons you put in it. You could just drop normal
JButtons in, or call add on an Action
(a special subclass of ActionListener that includes
info on labels and icons), which automatically creates a JButton.
The problem in both cases is that a graphical button for a toolbar differs
in two ways from normal JButtons:
JButton maintains
relatively large margins. Solution: call setMargin with
an Insets object with all values zero (or at least small).
JButton puts text labels to the right of the icon, but toolbars
usually have the label below. Solution: call
setVerticalTextPosition(BOTTOM) and
setHorizontalTextPosition(CENTER).
Since I want to do the above two steps for virtually all buttons I ever
put in a JToolBar, I defined a small class that
extends JButton with these modifications.
import java.awt.*;
import javax.swing.*;
public class ToolBarButton extends JButton {
private static final Insets margins =
new Insets(0, 0, 0, 0);
public ToolBarButton(Icon icon) {
super(icon);
setMargin(margins);
setVerticalTextPosition(BOTTOM);
setHorizontalTextPosition(CENTER);
}
public ToolBarButton(String imageFile) {
this(new ImageIcon(imageFile));
}
public ToolBarButton(String imageFile, String text) {
this(new ImageIcon(imageFile));
setText(text);
}
}
This example makes a simple JFrame with a JToolBar
at the top. The toolbar is intended to look somewhat like one that might
come with a simple Web browser. Text labels are initially off, but can
be toggled on and off by the use of a JCheckBox.
| JToolbar on Top (Original Configuration) | JToolbar on Left (After Drag/Drop) | Floating JToolbar (After Drag) | JToolbar with Text Labels (After Selecting JCheckBox) |
|---|---|---|---|
|
|
|
|
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JToolBarExample extends JFrame
implements ItemListener {
private BrowserToolBar toolbar;
private JCheckBox labelBox;
public static void main(String[] args) {
new JToolBarExample();
}
public JToolBarExample() {
super("JToolBar Example");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.white);
toolbar = new BrowserToolBar();
content.add(toolbar, BorderLayout.NORTH);
labelBox = new JCheckBox("Show Text Labels?");
labelBox.setHorizontalAlignment(SwingConstants.CENTER);
labelBox.addItemListener(this);
content.add(new JTextArea(10,30), BorderLayout.CENTER);
content.add(labelBox, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public void itemStateChanged(ItemEvent event) {
toolbar.setTextLabels(labelBox.isSelected());
pack();
}
}
Note: also requires
WindowUtilities.java
and ExitListener.java,
shown earlier.
import java.awt.*;
import javax.swing.*;
public class BrowserToolBar extends JToolBar {
public BrowserToolBar() {
String[] imageFiles =
{ "Left.gif", "Right.gif", "RotCCUp.gif",
"TrafficRed.gif", "Home.gif", "Print.gif", "Help.gif" };
String[] toolbarLabels =
{ "Back", "Forward", "Reload", "Stop", "Home", "Print", "Help" };
Insets margins = new Insets(0, 0, 0, 0);
for(int i=0; i<toolbarLabels.length; i++) {
ToolBarButton button =
new ToolBarButton("images/" + imageFiles[i]);
button.setToolTipText(toolbarLabels[i]);
button.setMargin(margins);
add(button);
}
}
public void setTextLabels(boolean labelsAreEnabled) {
Component c;
int i = 0;
while((c = getComponentAtIndex(i++)) != null) {
ToolBarButton button = (ToolBarButton)c;
if (labelsAreEnabled)
button.setText(button.getToolTipText());
else
button.setText(null);
}
}
}
Note: in addition to ToolBarButton.java (shown above), this requires
various GIF images.