• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Swing » JToolBar in Java Swing Example
Next →
← Prev

JToolBar in Java Swing Example

By Dinesh Thakur

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

Description

JtoolBar()

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();
        BtnOne.addActionListener(this);
        BtnTwo.addActionListener(this);
        CmbBox.addItem("Item One");
        CmbBox.addItem("Item Two");
        CmbBox.addItem("Item Three");
        CmbBox.addItem("Item Four");
        CmbBox.addItemListener(this);
        TlBr.add(BtnOne);
        TlBr.addSeparator();
        TlBr.add(BtnTwo);
        TlBr.addSeparator();
        TlBr.add(CmbBox);
        //jcombobox.setMaximumSize(jcombobox.getPreferredSize());
        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();
                      else
                      OutStrng += "De Selected: " + (String)e2.getItem();
                      showStatus(OutStrng);
                   }
 }
/*<APPLET CODE =JavaExampleToolBarInJApplet.class WIDTH=500 HEIGHT=280> </APPLET>*/

JToolBar in Java Swing Example

You’ll also like:

  1. JList in Java Swing Example
  2. SpringLayout in Java Swing Example
  3. FocusListener in Java Swing Example
  4. CheckerBoard Java Swing Example
  5. Gradient in Java Swing Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW