Java Swing | JToolBar

JToolBar is a part of Java Swing package. JToolBar is an implementation of toolbar. The JToolBar is a group of commonly used components such as buttons or drop down menu. 
JToolBar can be dragged to different locations by the user 
Constructors of the class are:
- JToolBar() : creates a new toolbar with horizontal orientation
 - JToolBar(int o) : creates a new toolbar with specified orientation
 - JToolBar(String n) : creates a new toolbar with specified name
 - JToolBar(String n, int o) : creates a new toolbar with specified name and orientation.
 
Commonly used methods :
- addSeparator() : adds the separator to the end of toolbar
 - setFloatable(boolean b) : if true is passed then the toolbar can be dragged to other locations or else not .
 - setLayout(LayoutManager m) : set layout of the toolbar
 - setOrientation(int o) : sets the orientation of toolbar
 - add(Component c) : adds component to the toolbar
 - getMargin() : returns the margin of the toolbar
 - setMargin(Insets m) : sets the margin of the toolbar to the insets given
 - getOrientation() : returns the orientation of the toolbar
 - updateUI() : Notification from the UIFactory that the Look and feel has changed.
 - setUI(ToolBarUI u) : Sets the Look and feel object that renders this component.
 - setRollover(boolean b) : Sets the rollover state of this toolbarto boolean b.
 - setFloatable(boolean b) :the boolean b decides whether the position of toolbar can be changed or not
 - setBorderPainted(boolean b): decides whether border should be painted or not.
 - paintBorder(Graphics g) : paint the toolbar’s border
 - isRollover() : Returns the rollover state.
 - isFloatable() : returns the floatable property.
 - isBorderPainted() : returns whether border is painted or not
 - getComponentIndex(Component c) : Returns the index of the specified component.
 - getComponentAtIndex(int i) :Returns the component at the specified index.
 - addSeparator(Dimension size) :Appends a separator of a specified dimension.
 - addSeparator() :Appends a separator of default size.
 - add(Action a) : adds a new JButton that follows the specified action.
 - paramString() : returns a string representation of this JToolBar.
 - getUIClassID() : returns the name of the Look and feel class that renders this component.
 - getAccessibleContext() : gets the AccessibleContext associated with this JToolBar.
 
The Following programs will illustrate the use of toolbar.
1. Program to create a simple toolbar and add buttons and combobox to it.  
Java
// Java Program to create a simple toolbar and add buttons and combobox to it.import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Tool extends JFrame {    // toolbar    static JToolBar tb;    // buttons    static JButton b1, b2;    // create a frame    static JFrame f;    // create a combo box    static JComboBox x;    public static void main()    {        // create a frame        f = new JFrame("Toolbar demo");        // set layout for frame        f.setLayout(new BorderLayout());        // create a toolbar        tb = new JToolBar();        // create a panel        JPanel p = new JPanel();        // create a combobox        x = new JComboBox(new String[] { "item 1", "item 2", "item 3" });        // create new buttons        b1 = new JButton("button 1");        b2 = new JButton("button 2");        // add buttons        p.add(b1);        p.add(b2);        // add menu to menu bar        p.add(x);        tb.add(p);        // add toolbar to frame        f.add(tb, BorderLayout.NORTH);        // set the size of the frame        f.setSize(500, 500);        f.setVisible(true);    }} | 
Output :
2. Program to create a toolbar and add action listener to its components .
Java
// Java Program to create a toolbar and add action listener to its components .import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Tool extends JFrame implements ActionListener, ItemListener {    // toolbar    static JToolBar tb;    // buttons    static JButton b1, b2;    // create a frame    static JFrame f;    // create a combo box    static JComboBox x;    // create a label    static JLabel l, l1;    public static void main()    {        // create a object of class        Tool to = new Tool();        // create a label        l = new JLabel("nothing selected");        l1 = new JLabel("nothing selected");        // create a frame        f = new JFrame("Toolbar demo");        // set layout for frame        f.setLayout(new BorderLayout());        // create a toolbar        tb = new JToolBar();        // create a panel        JPanel p = new JPanel();        // create a combobox        x = new JComboBox(new String[] { "item 1", "item 2", "item 3" });        // add actionListener        x.addItemListener(to);        // create new buttons        b1 = new JButton("button 1");        b2 = new JButton("button 2");        // add ActionListener to it        b1.addActionListener(to);        b2.addActionListener(to);        // add buttons        p.add(b1);        p.add(b2);        // add menu to menu bar        p.add(x);        tb.add(p);        // create a panel        JPanel p1 = new JPanel();        p1.add(l);        p1.add(l1);        // add toolbar to frame        f.add(tb, BorderLayout.NORTH);        f.add(p1, BorderLayout.CENTER);        // set the size of the frame        f.setSize(500, 500);        f.setVisible(true);    }    // if button is pressed    public void actionPerformed(ActionEvent e)    {        l.setText(e.getActionCommand() + " selected.");    }    // if combo box is selected    public void itemStateChanged(ItemEvent e)    {        l1.setText(x.getSelectedItem() + " selected.");    }} | 
Output :
3. Program to create a vertical toolbar and add action listener to its components .
Java
import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Tool extends JFrame implements ActionListener, ItemListener {    // toolbar    static JToolBar tb;    // buttons    static JButton b1, b2;    // create a frame    static JFrame f;    // create a combo box    static JComboBox x;    // create a label    static JLabel l, l1;    public static void main()    {        // create a object of class        Tool to = new Tool();        // create a label        l = new JLabel("nothing selected");        l1 = new JLabel("nothing selected");        // create a frame        f = new JFrame("Toolbar demo");        // set layout for frame        f.setLayout(new BorderLayout());        // create a toolbar        tb = new JToolBar("toolbar");        // set orientation        tb.setOrientation(SwingConstants.VERTICAL);        // create a panel        JPanel p = new JPanel();        // set layout        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));        // create a combobox        x = new JComboBox(new String[] { "item 1", "item 2", "item 3" });        // add actionListener        x.addItemListener(to);        // create new buttons        b1 = new JButton("button 1");        b2 = new JButton("button 2");        // add ActionListener to it        b1.addActionListener(to);        b2.addActionListener(to);        // add buttons        p.add(b1);        p.add(b2);        // add menu to menu bar        p.add(x);        tb.add(p);        // create a panel        JPanel p1 = new JPanel();        p1.add(l);        p1.add(l1);        // add toolbar to frame        f.add(tb, BorderLayout.WEST);        f.add(p1, BorderLayout.CENTER);        // set the size of the frame        f.setSize(500, 500);        f.setVisible(true);    }    // if button is pressed    public void actionPerformed(ActionEvent e)    {        l.setText(e.getActionCommand() + " selected.");    }    // if combo box is selected    public void itemStateChanged(ItemEvent e)    {        l1.setText(x.getSelectedItem() + " selected.");    }} | 
Output :
Note : The following programs might not run in an online compiler please use an Offline IDE
 
				
					



