by Dinesh Thakur Category: Swing

JToolBar: A container that allows you to group other components, usually buttons with icons in a row or column. Bars tools are unique in that the user can place them in different configurations on the main frame.

In Program, the toolbox is created using the class JButtons by passing the icons as the parameters of the constructors. Moreover, the icons can be set after creating the buttons also. Another option that can be used in Jbutton is that of setting the icon roll-over. Icon roll-over ensures that the icon changes when the mouse rolls over the tool bar. Table gives constructors and methods of the JToolBar class.

                          Constructors and methods of the JToolBar class.


Constructors and Methods



Constructs a new toolbar.

JtoolBar(int orientation)

Constructs a new toolbar with orientation specified by the parameter.

JtoolBar(String str)

Constructs a new toolbar with title specified by the parameter.

JToolBar(String str, int orientation)

Constructs a new toolbar with the title and orientation specified by the parameters.

JButton add (Action a)

Adds a JButton object that initializes the specified action.

void addSeparator()

Adds or appends a separator of default dimension to the end of the toolbar.

void addSeparator(dimension dim)

Adds or appends a separator of specified dimension (that is, the dimension specified by the parameter passed to this function) to the end of this toolbar.

Component getComponentAtlndex (int index)

Returns the component at the index specified by the parameter.

Component getComponentAtindex (int index)

Returns the component at the index specified by the parameter

int getComponentindex (Component c)

Returns the index of the component specified by the parameter.

Insets getMargin()

Returns the margin between the border of the toolbar and its buttons.

void remove(Component c)

Removes the component specified in the parameter from the toolbar.

void setMargin(lnsets insets)

Sets the margin specified by the parameter, between the tool bar 's border and its buttons.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleToolBarInJApplet extends JApplet implements ActionListener,ItemListener
    JButton BtnOne = new JButton("First Button", new ImageIcon("Koala.jpg"));
    JButton BtnTwo = new JButton("Second Button", new ImageIcon("Koala.jpg"));
    JComboBox CmbBox = new JComboBox();
    public void init()
        Container Cntnr = getContentPane();
        JToolBar TlBr = new JToolBar();
        CmbBox.addItem("Item One");
        CmbBox.addItem("Item Two");
        CmbBox.addItem("Item Three");
        CmbBox.addItem("Item Four");
        Cntnr.add(TlBr, BorderLayout.NORTH);
        public void actionPerformed(ActionEvent e1)
          if(e1.getSource() == BtnOne)
              showStatus("You Pressed Button One");
                 else if (e1.getSource() == BtnTwo)
                        showStatus("You Pressed Button Two");
                  public void itemStateChanged(ItemEvent e2)
                      String OutStrng = "";
                      if(e2.getStateChange() == ItemEvent.SELECTED)
                      OutStrng += "Selected: " + (String)e2.getItem();
                      OutStrng += "De Selected: " + (String)e2.getItem();
/*<APPLET CODE =JavaExampleToolBarInJApplet.class WIDTH=500 HEIGHT=280> </APPLET>*/

JToolBar in Java Swing Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles