Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.19. Tool bars

5.19. Tool bars
Last updated: 25 January 2013.

This tutorial will show you how to use tool bars in Java.

A tool bar is an instance of the class JToolBar. It is a component that generally contains a set of buttons with icons and no text. If there is a menu in your application, you can use a tool bar to provide quick access to the most commonly used menu items, that is, each button in the tool bar is like a shortcut for a menu item. The following picture shows a frame containing a tool bar with 5 buttons. Each button has a colored icon. When the user clicks a button, the panel at the center of the frame (below the tool bar) is filled with the corresponding color.


Tool bar example



A tool bar can be dragged to the top, left, right, bottom edge of its container or outside in a separate window, provided that the layout is BorderLayout. Typically, if you want the tool bar to be draggable, you set the layout of the main container to BorderLayout and place the tool bar at one of the edges of the container. Usually, the buttons in the tool bar act upon a component that is placed at the center of the container. Note that except the component placed at the center, the tool bar must be the only other component of the container and it must not be placed at the center. Otherwise, the tool bar will not be draggable. If you don't want the tool bar to be draggable, call the method setFloatable(false). In the picture shown below, the tool bar has been dragged to the bottom of the frame:


Another tool bar example



Here is the code that builds the frame shown in the above examples:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public final class ToolBars extends JFrame implements ActionListener {

   
private JButton redButton;
   
private JButton greenButton;
   
private JButton blueButton;
   
private JButton yellowButton;
   
private JButton magentaButton;

   
private JPanel centerPanel;

   
public ToolBars() {
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
public void actionPerformed(ActionEvent e){
         
if(e.getSource().equals(redButton))
               
centerPanel.setBackground(Color.RED);
         
else if(e.getSource().equals(greenButton))
               
centerPanel.setBackground(Color.GREEN);
         
else if(e.getSource().equals(blueButton))
               
centerPanel.setBackground(Color.BLUE);
         
else if(e.getSource().equals(yellowButton))
               
centerPanel.setBackground(Color.YELLOW);
         
else if(e.getSource().equals(magentaButton))
               
centerPanel.setBackground(Color.MAGENTA);
   
}


   
private void addComponents() {
         
// Set up the content pane
         
JPanel contentPane = new JPanel(new BorderLayout());

         
// Create the tool bar
         
JToolBar toolBar = new JToolBar("My JToolBar");

         
// Set up the red button
         
redButton = new JButton(new ImageIcon("../images/icon-red.gif"));
          redButton.setToolTipText
("Set the center panel to red");
          redButton.addActionListener
(this);
          toolBar.add
(redButton);

         
// Set up the green button
         
greenButton = new JButton(new ImageIcon("../images/icon-green.gif"));
          greenButton.setToolTipText
("Set the center panel to green");
          greenButton.addActionListener
(this);
          toolBar.add
(greenButton);

         
// Set up the blue button
         
blueButton = new JButton(new ImageIcon("../images/icon-blue.gif"));
          blueButton.setToolTipText
("Set the center panel to blue");
          blueButton.addActionListener
(this);
          toolBar.add
(blueButton);

         
// Add a separator to the tool bar
         
toolBar.addSeparator();

         
// Set up the yellow button
         
yellowButton = new JButton(new ImageIcon("../images/icon-yellow.gif"));
          yellowButton.setToolTipText
("Set the center panel to yellow");
          yellowButton.addActionListener
(this);
          toolBar.add
(yellowButton);

         
// Set up the magenta button
         
magentaButton = new JButton(new ImageIcon("../images/icon-magenta.gif"));
          magentaButton.setToolTipText
("Set the center panel to magenta");
          magentaButton.addActionListener
(this);
          toolBar.add
(magentaButton);

         
// Add the tool bar to the content pane
         
add(toolBar, BorderLayout.PAGE_START);

         
// Create the center panel and add it to the content pane
         
centerPanel = new JPanel();
          contentPane.add
(centerPanel, BorderLayout.CENTER);

         
// Add the content pane to the JFrame
         
add(contentPane);
   
}


   
private void init() {
         
try{
               
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
         
}
         
catch(Exception e){
               
e.printStackTrace();
         
}
         
setTitle("ToolBars");
          setSize
(500, 300);
          setLocationRelativeTo
(null);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new ToolBars();
               
}
          })
;
   
}
}


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.19. Tool bars
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.20. Combo boxes

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7