Saturday, September 6, 2025
HomeLanguagesJavaJava Swing | JToolBar

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: 

  1. JToolBar() : creates a new toolbar with horizontal orientation
  2. JToolBar(int o) : creates a new toolbar with specified orientation
  3. JToolBar(String n) : creates a new toolbar with specified name
  4. JToolBar(String n, int o) : creates a new toolbar with specified name and orientation.

Commonly used methods :  

  1. addSeparator() : adds the separator to the end of toolbar
  2. setFloatable(boolean b) : if true is passed then the toolbar can be dragged to other locations or else  not .
  3. setLayout(LayoutManager m) : set layout of the toolbar
  4. setOrientation(int o) : sets the orientation of toolbar
  5. add(Component c) : adds component to the toolbar
  6. getMargin() : returns the margin of the toolbar
  7. setMargin(Insets m) : sets the margin of the toolbar to the insets given
  8. getOrientation() : returns the orientation of the toolbar
  9. updateUI() : Notification from the UIFactory that the Look and feel has changed.
  10. setUI(ToolBarUI u) : Sets the Look and feel object that renders this component.
  11. setRollover(boolean b) : Sets the rollover state of this toolbarto boolean b.
  12. setFloatable(boolean b) :the boolean b decides whether the position of toolbar can be changed or not
  13. setBorderPainted(boolean b): decides whether border should be painted or not.
  14. paintBorder(Graphics g) : paint the toolbar’s border
  15. isRollover() : Returns the rollover state.
  16. isFloatable() : returns the floatable property.
  17. isBorderPainted() : returns whether border is painted or not
  18. getComponentIndex(Component c) : Returns the index of the specified component.
  19. getComponentAtIndex(int i) :Returns the component at the specified index.
  20. addSeparator(Dimension size) :Appends a separator of a specified dimension.
  21. addSeparator() :Appends a separator of default size.
  22. add(Action a) : adds a new JButton that follows the specified action.
  23. paramString() : returns a string representation of this JToolBar.
  24. getUIClassID() : returns the name of the Look and feel class that renders this component.
  25. 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
 

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS