Java JToolBar

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JToolBar

JToolBar container allows us to group other components, usually buttons with icons in a row or column. JToolBar provides a component which is useful for displaying commonly used actions or controls.

Nested Classes

Modifier and Type Class Description
protected class JToolBar.AccessibleJToolBar This class implements accessibility support for the JToolBar class.
static class JToolBar.Separator A toolbar-specific separator.


Constructor Description
JToolBar() It creates a new tool bar; orientation defaults to HORIZONTAL.
JToolBar(int orientation) It creates a new tool bar with the specified orientation.
JToolBar(String name) It creates a new tool bar with the specified name.
JToolBar(String name, int orientation) It creates a new tool bar with a specified name and orientation.

Useful Methods

Modifier and Type Method Description
JButton add(Action a) It adds a new JButton which dispatches the action.
protected void addImpl(Component comp, Object constraints, int index) If a JButton is being added, it is initially set to be disabled.
void addSeparator() It appends a separator of default size to the end of the tool bar.
protected PropertyChangeListener createActionChangeListener(JButton b) It returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur, or null if the default property change listener for the control is desired.
protected JButton createActionComponent(Action a) Factory method which creates the JButton for Actions added to the JToolBar.
ToolBarUI getUI() It returns the tool bar's current UI.
void setUI(ToolBarUI ui) It sets the L&F object that renders this component.
void setOrientation(int o) It sets the orientation of the tool bar.

Java JToolBar Example

  1. import java.awt.BorderLayout;  
  2. import java.awt.Container;  
  3. import javax.swing.JButton;  
  4. import javax.swing.JComboBox;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.JScrollPane;  
  7. import javax.swing.JTextArea;  
  8. import javax.swing.JToolBar;  
  10. public class JToolBarExample {  
  11.     public static void main(final String args[]) {  
  12.         JFrame myframe = new JFrame("JToolBar Example");  
  13.         myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  14.         JToolBar toolbar = new JToolBar();  
  15.         toolbar.setRollover(true);  
  16.         JButton button = new JButton("File");  
  17.         toolbar.add(button);  
  18.         toolbar.addSeparator();  
  19.         toolbar.add(new JButton("Edit"));  
  20.         toolbar.add(new JComboBox(new String[] { "Opt-1""Opt-2""Opt-3""Opt-4" }));  
  21.         Container contentPane = myframe.getContentPane();  
  22.         contentPane.add(toolbar, BorderLayout.NORTH);  
  23.         JTextArea textArea = new JTextArea();  
  24.         JScrollPane mypane = new JScrollPane(textArea);  
  25.         contentPane.add(mypane, BorderLayout.EAST);  
  26.         myframe.setSize(450250);  
  27.         myframe.setVisible(true);  
  28.     }  
  29. }  


Java Jtoolbar