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");
toolBar.add(newButton);

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

class  ExitAction extends AbstractAction {
    public ExitAction(String action)  {
        super(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);
    }
    @Override
    public void  actionPerformed(ActionEvent e)  { 
        System.exit(0);
    }
}

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");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    JToolBar toolbar = new JToolBar();
    Icon icon = MetalIconFactory.getFileChooserDetailViewIcon();
    JToggleButton button = new JToggleButton(icon);
    toolbar.add(button);
    icon = MetalIconFactory.getFileChooserHomeFolderIcon();
    button = new JToggleButton(icon);
    toolbar.add(button);
    icon = MetalIconFactory.getFileChooserListViewIcon();
    button = new JToggleButton(icon);
    toolbar.add(button);
    content.add(toolbar, BorderLayout.NORTH);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}




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"));

    toolbar.add(selectb);
    toolbar.add(freehandb);
    toolbar.add(shapeedb);
    toolbar.add(penb);

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

    f.setSize(250, 350);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}