How to Create ToolBar by Using JToolBar

Introduction to Toolbar

A toolbar provides users with commonly used features of the application. We usually place a toolbar directly below the menu bars at the top of a frame. A toolbar acts as a container for other components including button, combobox, and menu.

We often use the toolbar as a selection tool e.g., text alignment in office suites application. In addition, we use a toolbar to replace commonly used functions of menu to allow users to access features of the application more quickly. In some applications, you will find that we use a toolbar for navigation e.g., web browsers.

JToolBar class

In order to create a toolbar in Java Swing, you use JToolBar class. The JToolbar class supports two orientations: vertical and horizontal. You use the orientation attribute to maintain the current orientation of the toolbar.

You can add any component to the toolbar including a button, combobox, and menu. The order of components in the toolbar is determined by an integer index.

If you want to separate a group of related components in the toolbar, you can use JToolBar’s own separator by calling method addSeperator().

JToolBar also supports a floatable toolbar. If you don’t want the toolbar floatable, you can use the method setFloatable(false). 

It is highly recommended to place a toolbar in a container that supports BorderLayout. Because when you drag a toolbar, Swing will place the toolbar at either north, south, east, or west side of the container.

Here are constructors of JToolBar class:

ConstructorsDescription
JToolBar()Creates a new toolbar with default horizontal orientation.
JToolBar(int orientation)Creates a new toolbar with a given orientation.
JToolBar(String name)Creates a new toolbar with a given name.
JToolBar(String name, int orientation)Creates a new toolbar with a given name and orientation

JToolBar Demo Application

Here is the screenshot of the JToolBar demo application:

JToolBar Demo
package jtoolbardemo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main {

    public static void main(String[] args) {

        final JFrame frame = new JFrame("JToolBar Demo");
        JToolBar toolbar = new JToolBar("Applications");

        JButton btnCalendar = new JButton(new ImageIcon("images/Calendar.png"));
        btnCalendar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Calendar clicked");
            }
        });

        JButton btnClock = new JButton(new ImageIcon("images/Clock.png"));
        btnClock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Clock clicked");
            }
        });

        JButton btnContacts = new JButton(new ImageIcon("images/Contacts.png"));
        btnContacts.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Contact clicked");
            }
        });

        JButton btnMail = new JButton(new ImageIcon("images/Mail.png"));
        btnMail.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Mail clicked");
            }
        });

        JButton btnMessages = new JButton(new ImageIcon("images/Messages.png"));
        btnMessages.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Messages clicked");
            }
        });

        JButton btnPhone = new JButton(new ImageIcon("images/Phone.png"));
        btnPhone.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Phone clicked");
            }
        });

        toolbar.add(btnCalendar);
        toolbar.add(btnClock);
        toolbar.add(btnContacts);
        toolbar.add(btnMail);
        toolbar.add(btnMessages);
        toolbar.add(btnPhone);

        frame.setLayout(new BorderLayout());
        frame.getContentPane().add(toolbar, BorderLayout.PAGE_START);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(500, 200);
        frame.setVisible(true);

    }
}Code language: JavaScript (javascript)

In this tutorial, we have shown you how to use JToolBar class to create toolbars for the Java swing application.