Java JToolbar Class Example

JTOOLBAR

Java Swing Tutorial Explaining the JToolBar Component. A JToolbar contains a number of components whose type is usually some kind of button which can also include separators to group related components within the toolbar. The toolbar can be docked against any of the four edges of a container (panel or a frame). A toolbar can also be made to float.

Toolbars uses BoxLayout, which arranges components in one horizontal row/ vertical column. This layout manager does not force each component to have the same height or width; instead, it uses their preferred height or width, and attempts to align them.
You can adjust the resulting alignment by calling class Component’s methods setAlignmentX() and/or setAlignmentY() on each component.

JTOOLBAR SOURCE CODE

import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;

public class JToolBarDemo extends JFrame {

	protected JTextArea textArea;
	protected String newline = "\n";
	public JToolBarDemo() {
		super("ToolBarDemo");
		// Create the toolbar.
		JToolBar jtbMainToolbar = new JToolBar();
		// setFloatable(false) to make the toolbar non movable
		addButtons(jtbMainToolbar);
		// Create the text area
		textArea = new JTextArea(5, 30);
		JScrollPane jsPane = new JScrollPane(textArea);
		// Lay out the content pane.
		JPanel jplContentPane = new JPanel();
		jplContentPane.setLayout(new BorderLayout());
		jplContentPane.setPreferredSize(new Dimension(400, 100));
		jplContentPane.add(jtbMainToolbar, BorderLayout.NORTH);
		jplContentPane.add(jsPane, BorderLayout.CENTER);
		setContentPane(jplContentPane);
	}
	public void addButtons(JToolBar jtbToolBar) {
		JButton jbnToolbarButtons = null;
		// first button
		jbnToolbarButtons = new JButton(new ImageIcon("left.gif"));
		jbnToolbarButtons.setToolTipText("left");
		jbnToolbarButtons.addActionListener(new ActionListener() {

		     public void actionPerformed(ActionEvent e) {
			displayInTextArea("This is Left Toolbar Button Reporting");
			}
		});
		jtbToolBar.add(jbnToolbarButtons);
		// 2nd button
		jbnToolbarButtons = new JButton(new ImageIcon("right.gif"));
		jbnToolbarButtons.setToolTipText("right");
		jbnToolbarButtons.addActionListener(new ActionListener() {

		     public void actionPerformed(ActionEvent e) {
			displayInTextArea("This is right Toolbar Button Reporting");
			}
		});
		jtbToolBar.add(jbnToolbarButtons);
		jtbToolBar.addSeparator();
		// 3rd button
		jbnToolbarButtons = new JButton(new ImageIcon("open.gif"));
		jbnToolbarButtons.setToolTipText("open");
		jbnToolbarButtons.addActionListener(new ActionListener() {

		     public void actionPerformed(ActionEvent e) {
			displayInTextArea("This is open Toolbar Button Reporting");
			}
		});
		jtbToolBar.add(jbnToolbarButtons);
		// 4th button
		jbnToolbarButtons = new JButton(new ImageIcon("save.gif"));
		jbnToolbarButtons.setToolTipText("save");
		jbnToolbarButtons.addActionListener(new ActionListener() {

		     public void actionPerformed(ActionEvent e) {
			displayInTextArea("This is save Toolbar Button Reporting");
			}
		});
		jtbToolBar.add(jbnToolbarButtons);
		// We can add separators to group similar components
		jtbToolBar.addSeparator();
		// fourth button
		jbnToolbarButtons = new JButton("Text button");
		jbnToolbarButtons.addActionListener(new ActionListener() {

		     public void actionPerformed(ActionEvent e) {
			displayInTextArea("Text button");
			}
		});
		jtbToolBar.add(jbnToolbarButtons);
		// fifth component is NOT a button!
		JTextField jtfButton = new JTextField("Text field");
		jtfButton.setEditable(false);
		jtfButton.addActionListener(new ActionListener() {

		     public void actionPerformed(ActionEvent e) {
			displayInTextArea("TextField component can also be placed");
			}
		});
		jtbToolBar.add(jtfButton);
	}
	protected void displayInTextArea(String actionDescription) {
		textArea.append(actionDescription + newline);
	}
	public static void main(String[] args) {
		JToolBarDemo jtfToolbar = new JToolBarDemo(); // Extends Frame.
		jtfToolbar.pack();
		jtfToolbar.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		jtfToolbar.setVisible(true);
	}
}

Output

Download JToolbar Source Code

JAVA JTOOLBAR HIERARCHY

javax.swing
Class JToolBar 
java.lang.Object
java.awt.BorderLayout
All Implemented Interfaces: 
LayoutManager, LayoutManager2, Serializable

JTOOLBAR CONSTRUCTOR

JToolBar()
Creates a new tool bar; orientation defaults to HORIZONTAL.

JToolBar(int orientation)
Creates a new tool bar with the specified orientation.

JToolBar(String name)
Creates a new tool bar with the specified name.

JToolBar(String name, int orientation)
Creates a new tool bar with a specified name and orientation.

Like us on Facebook