CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JToolBar in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel
  • Home

JToolBar in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

A JToolBar in Java Swing is a graphical component used to create toolbars in GUI applications. Toolbars typically contain buttons and other controls for quick access to frequently used actions. Following is a basic example demonstrating the usage of JToolBar:

import java.awt.BorderLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

public class JToolBarDemo extends JFrame {
	public JToolBarDemo() {
		setTitle("JToolBar Demo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create a toolbar
		JToolBar toolBar = new JToolBar();

		final int toolBarIconWidth = 20;
		final int toolBarIconHeight = toolBarIconWidth;

		// Create buttons for the toolbar
		ImageIcon openIcon = new ImageIcon("open.png");
		openIcon = new ImageIcon(
				openIcon.getImage().getScaledInstance(toolBarIconWidth, toolBarIconHeight, Image.SCALE_SMOOTH));
		JButton openButton = new JButton(openIcon);

		ImageIcon saveIcon = new ImageIcon("save.png");
		saveIcon = new ImageIcon(
				saveIcon.getImage().getScaledInstance(toolBarIconWidth, toolBarIconHeight, Image.SCALE_SMOOTH));
		JButton saveButton = new JButton(saveIcon);

		ImageIcon printIcon = new ImageIcon("print.png");
		printIcon = new ImageIcon(
				printIcon.getImage().getScaledInstance(toolBarIconWidth, toolBarIconHeight, Image.SCALE_SMOOTH));
		JButton printButton = new JButton(printIcon);

		// Add buttons to the toolbar
		toolBar.add(openButton);
		toolBar.add(saveButton);
		toolBar.add(printButton);

		// Add the toolbar to the frame
		add(toolBar, BorderLayout.NORTH);

		setSize(300, 200);
		setLocationRelativeTo(null); // Center the window
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> {
			new JToolBarDemo().setVisible(true);
		});
	}

}

Output:

JToolBar in java swing

Note: To make it work with the icon, save your icon images in the same directory where JToolBarDemo.java file is there and it should work.

If it does not work, feel free to write a comment. I will try to response as earlier as I can.

Thank you.

Related Posts:

  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Control Statements in Java
  • Java Swing Tutorial
  • Command Line Arguments in Java
  • Identifiers in Java
  • MySQL Commands for Developers
Tags:guijavaswing
Was this article helpful?
← Previous ArticleJMenuBar in Java Swing
Next Article →JPasswordField in Java Swing

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap