Menus and ToolBar in Java Swing

A tool bar is a button bar that gives quick access to the most commonly used commands in a program.Tool bar are made up of buttons,there is no restriction on the components that you can add to a tool bar.

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.beans.*;

public class ToolBarTest

{

   public static void main(String aa[])

   {

       JFrame frame=new ToolBarFrame();

       frame.show();

   }

}

/*the color action sets the background of its target to a given color*/

class ColorActioon extends AbstractAction

{

   public ColorActioon(String namee, Icon icon,Color c, Component t)

   {

       putValue(Action.NAME,namee);

       putValue(Action.SMALL_ICON,icon);

       putValue(Action.SHORT_DESCRIPTION,namee+" background");

       putValue("Color",c);

       target=t;

     }

   public void actionPerformed(ActionEvent acevt)

   {

       Color co=(Color)getValue("Coloor");

       target.setBackground(co);

       target.repaint();

   }

   private Component target;

}

/*the toolbar button is a button with an icon and no text suitable for addition into a tool bar. The tool tip is set to the short description of the action, or to the name if the short description is not available*/

class ToolBarButton extends JButton

{

   public ToolBarButton(Action a)

   {

       super((Icon)a.getValue(Action.SMALL_ICON));

       String toolTip=(String)a.getValue(Action.SHORT_DESCRIPTION);

       if(toolTip==null)

               toolTip=(String)a.getValue(Action.NAME);

       if(toolTip!=null)

             setToolTipText(toolTip);

       addActionListener(a);

     }

}

class ToolBarFrame extends JFrame

{

     public ToolBarFrame()

       {

         setTitle("TooBarTest");

         setSize(300,300);

      

          //add a panel (at center location)for color change

       Container contentPanee=getContentPane();

         JPanel panel=new JPanel();

         contentPanee.add(panel,"Center");

         //set up actions

      Action blueActioon=new ColorAction("Bluee",new ImageIcon("bluee.gif"),Color.blue, panel);

    Action yellowActioon=new ColorAction("Yelloww",new ImageIcon("yelloow.jpg"),Color.yellow, panel);

     Action redActioon=new ColorAction("Redd",new ImageIcon("redd.gif"),Color.red, panel);

    Action exitActioon=new AbstractAction("Exit",new ImageIcon("exitt.gif"))

         {

             public void actionPerformed(ActionEvent actionevn)

               {

                   System.exit(0);

                 }

         };

       //populate tool bar(add components into the toolbar)

           JToolBar bar=new JToolBar();

           bar.add(new ToolBarButton(blueActioon));

           bar.add(new ToolBarButton(yellowActioon));

           bar.add(new ToolBarButton(redActioon));

           bar.addSeparator();

           bar.add(new ToolBarButton(exitActioon));

           contentPanee.add(bar,"North");

         //populate menu

           JMenu menu=new JMenu("Coloor");

           menu.add(yellowActioon);

           menu.add(blueActioon);

           menu.add(redActioon);

           menu.add(exitActioon);

           JMenuBar menuBaar=new JMenuBar();

           menuBaar.add(menu);

           setJMenuBar(menuBaar);

   }

}

Toolbar with action in swing

Explanation…

A tooltip is activated when the cursor rests for a moment over a button. The tool tip text is displayed inside a colored rectangle.When the user moves the mouse away, the tool tip is removed.

Void addseparator()—adds a separator to the end of the tool bar.

Void setToolTipText(String text)—sets the text that should be displayed as a too tip when the mouse hovers over the components.