Java Swing Tutorial - Java Swing JToolBar

A toolbar hosts buttons that provides commonly used actions to the user in a JFrame.

JToolBar class represents a toolbar and acts as a container for the toolbar buttons.

The following code creates some toolbar components:

Creates a horizontal JToolBar

JToolBar   toolBar = new JToolBar();

The following code creates a horizontal JToolBar with a title. The title is displayed as a window title, when it floats in a separate window.

JToolBar   toolBarWithTitle = new JToolBar("My  ToolBar  Title");

To create a Vertical toolbar

JToolBar   vToolBar  = new JToolBar(JToolBar.VERTICAL);

The following code adds buttons to the toolbar. We make a JButton smaller by setting its margin to zero.

JButton newButton  = new JButton("New");
newButton.setMargin(new Insets(0, 0, 0, 0));
newButton.setToolTipText("Add a new file");

The following code uses Action to link menu item, button on toolbar.

class  ExitAction extends AbstractAction {
    public ExitAction(String action)  {
        // Set  tooltip text for the   toolbar 
        this.putValue(SHORT_DESCRIPTION, "Exit the   application");
        // Set  a  mnemonic key 
        this.putValue(MNEMONIC_KEY,  KeyEvent.VK_E);
    public void  actionPerformed(ActionEvent e)  { 

ExitAction exitAction  = new ExitAction("Exit"); 
JButton exitButton  = new JButton(ExitAction); 
JMenuItem exitMenuItem = new JMenuItem(exitAction);

JButton exitToolBarButton = new JButton(exitAction);
exitToolBarButton.setMargin(new Insets(0,0,0,0));

To disable the exit option, just calling exitAction.setEnabled(false) and both the button and menu and the button on toolbar would be disabled.

import java.awt.BorderLayout;
import java.awt.Container;
/*from  w w  w  .  ja v a2  s  . c o  m*/
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalIconFactory;

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("JToolbar Sample");
    Container content = f.getContentPane();
    JToolBar toolbar = new JToolBar();
    Icon icon = MetalIconFactory.getFileChooserDetailViewIcon();
    JToggleButton button = new JToggleButton(icon);
    icon = MetalIconFactory.getFileChooserHomeFolderIcon();
    button = new JToggleButton(icon);
    icon = MetalIconFactory.getFileChooserListViewIcon();
    button = new JToggleButton(icon);
    content.add(toolbar, BorderLayout.NORTH);
    f.setSize(300, 100);

Shows a vertical toolbar

import java.awt.BorderLayout;
//from w w w . j  a  va2s  .c o m
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

public class Main {
  public static void main(String[] args) {
    JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);

    JButton selectb = new JButton(new ImageIcon("select.gif"));
    JButton freehandb = new JButton(new ImageIcon("freehand.gif"));
    JButton shapeedb = new JButton(new ImageIcon("shapeed.gif"));
    JButton penb = new JButton(new ImageIcon("pen.gif"));


    JFrame f = new JFrame();
    f.add(toolbar, BorderLayout.WEST);

    f.setSize(250, 350);