Table of contents
1.
Introduction
2.
Nested Classes
3.
Constructors
4.
Commonly Used Methods
5.
Example
6.
Frequently Asked Questions
6.1.
In Java, what is JToolBar?
6.2.
What's the best way to make a Java toolbar swing?
6.3.
In Java, what is the JFrame class?
6.4.
In Java Swing, what is JscrollPane?
6.5.
In Java, how does the toolbar look?
6.6.
What's the difference between JScrollPane and JScrollBar, and how do you use them?
7.
Conclusion
Last Updated: Mar 27, 2024

JToolBar

Author Mayank Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JToolBar is a Java Swing component. JToolBar is a toolbar implementation. The JToolBar is a collection of standard components, including buttons and drop-down menus. The JToolBar container allows us to arrange additional components in a row or column, with usual buttons with iconography. JToolBar is a component that can display frequently used actions or controls. The user can drag JToolBar to multiple locations.

Nested Classes

Modifier and Type Class Description
Protected class JToolBar.AccessibleJToolBar For the JToolBar class, this class implements accessibility support.
Static class JToolBar.Separator A toolbar-specific separator.

 

Constructors

In this article, we will study about JToolBar with its different functionalities.

JToolBar(): generates a new horizontally oriented toolbar.

 JToolBar(int o): Creates a new toolbar with the provided Orientation.

JToolBar(String n): this method generates a new toolbar with the given name.

JToolBar(String n, int o): Creates a new toolbar with the supplied name and Orientation.

Commonly Used Methods

  • addSeparator(): inserts a separator at the end of the toolbar.
  • addSeparator(): inserts a separator at the end of the toolbar. Floatable(boolean b): If true, the toolbar can be moved to different locations; else, it cannot.
  • setLayout(LayoutManager m): Set the layout of the toolbar.
  • set Orientation (int o): sets the toolbar's Orientation.
  • add(Component c): Component c is added to the toolbar.
  • getMargin(): returns the toolbar set's margin.
  • setMargin(Insets m): sets the toolbar's margin to the insets specified.
  • getOrientation(): returns the toolbar's orientation.
  • updateUI(): UIFactory's notification that the look and feel have changed.
  •  setUI(ToolBarUI u): Sets the Look and Feel object that renders this component.
  • setRollover(boolean b): Sets the toolbar's rollover state to boolean b.
  • setFloatable(boolean b): boolean b determines whether or not the toolbar's position can be adjusted.
  • setBorderPainted(boolean b): determines whether or not the border should be painted.
  • paintBorder(Graphics g): Paint the border of the toolbar
  • Rollover(): returns the current state of the rollover.
  • isFloatable(): returns whether or not a variable is floatable.
  • isBorderPainted(): returns whether or not the border has been painted.
  • getComponentIndex(Component c): Returns the index of the supplied component.
  • getComponentAtIndex(int i): Returns the component at the specified index.
  • addSeparator(Dimension size): Adds a separator of the supplied dimension to the list.
  • addSeparator(): Adds a separator of the specified size.
  • add(Action a): creates a new JButton that acts supplied.
  • paramString(): returns a string representation of iJToolBar.
  • getUIClassID(): retrieves the name of the component's Look and Feel class.
  • getAccessibleContext(): gets the AccessibleContext associated with this JToolBar.

Example

import java.awt.BorderLayout;  
import java.awt.Container;  
import javax.swing.JButton;  
import javax.swing.JComboBox;  
import javax.swing.JFrame;  
import javax.swing.JScrollPane;  
import javax.swing.JTextArea;  
import javax.swing.JToolBar;  
  
public class JToolBarExample 
{  
    public static void main(final String args[])
 {  
        JFrame myframe = new JFrame("Example of JToolBar");  
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        JToolBar toolbar = new JToolBar();  
        toolbar.setRollover(true);  
        JButton button = new JButton("File");  
        toolbar.add(button);  
        toolbar.addSeparator();  
        toolbar.add(new JButton("Edit"));  
        toolbar.add(new JComboBox(new String[] { "Opt-1", "Opt-2", "Opt-3", "Opt-4" }));  
        Container contentPane = myframe.getContentPane();  
        contentPane.add(toolbar, BorderLayout.NORTH);  
        JTextArea textArea = new JTextArea();  
        JScrollPane mypane = new JScrollPane(textArea);  
        contentPane.add(mypane, BorderLayout.EAST);  
        myframe.setSize(450, 250);  
        myframe.setVisible(true);  
    }  
} 

Frequently Asked Questions

In Java, what is JToolBar?

A JToolBar is a container that organizes numerous components into a row or column, with usual buttons with icons. Toolbars frequently allow quick access to features that are also available in menus. How to Use Actions explains how to give menu items and toolbar buttons the same purpose.

What's the best way to make a Java toolbar swing?

The JToolBar class is used to construct a toolbar in Java Swing. The JToolbar class supports vertical and horizontal orientations. The orientation parameter is used to keep the toolbar in its present position. Any component, such as a button, combobox, or menu, can be added to the toolbar.

In Java, what is the JFrame class?

JFrame is a container that inherits from java. awt. Frame class. A container is required whenever a Graphical User Interface (GUI) is constructed with Java Swing capabilities, known as JFrame. It is where components such as labels, buttons, and text fields are put to build a Graphical User Interface (GUI).

In Java Swing, what is JscrollPane?

A JscrollPane is used to make a component's view scrollable. We utilize a scroll pane to display a huge component or a component whose size can change dynamically when the screen size is constrained.

In Java, how does the toolbar look?

Set the layout of the toolbar using setLayout(LayoutManager m). setOrientation(int o): changes the toolbar's Orientation. Component c is added to the toolbar using add(Component c). getMargin() returns the toolbar's margin.

What's the difference between JScrollPane and JScrollBar, and how do you use them?

A JScrollBar is a component that does not handle its events, but a JScrollPane is a Container that does manage its events and scrolls.

Conclusion

In this article, we learn about JToolBar of Java Swing. This article looked into different nested classes, methods, and constructors used. Finally, I illustrated an example. That’s all from the article. I hope you all like it.

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

Live masterclass