Wednesday, April 27, 2016

Creating a Toolbar in Swing

In my earlier post I described how to add a JMenuBar with menu items and sub menu items. I am going to show how to add a tool bar to the same application under the menu bar as shown below.













I have highlighted the code to add the tool bar to the same JFrame.

 import javax.swing.*;  
 import java.awt.*;  
 public class ToolBarExampleMain extends JFrame{  
   public ToolBarExampleMain(){  
     super("Tool Bar Example App");           //sets the title for the JFrame  
     this.createInterface();  
   }  
   public void createInterface(){  
     setLayout(new BorderLayout());           //main Layout of the JFrame is BorderLayout  
     Toolkit tk = Toolkit.getDefaultToolkit();  
     int xSize = ((int) tk.getScreenSize().getWidth());  
     int ySize = ((int) tk.getScreenSize().getHeight());  
     setSize(xSize,ySize);                //setting the JFrame size according to the screen width and height  
     JPanel mainPanel = new JPanel(new BorderLayout());  
     mainPanel.add(createMenuBar(),BorderLayout.NORTH); //creating a JMenuBar and adding it on top of the frame  
     mainPanel.add(createToolBar(),BorderLayout.LINE_START);  
     add(mainPanel, BorderLayout.CENTER);  
     setVisible(true);  
   }  
   public JMenuBar createMenuBar(){  
     JMenuBar menuBar=new JMenuBar();  
     JMenu menuFile=new JMenu("File");          //File,Edit,View and Help are the main menus in the JMenuBar  
     JMenu menuEdit=new JMenu("Edit");  
     JMenu menuView=new JMenu("View");  
     JMenu menuHelp=new JMenu("Help");  
     JMenuItem subMenuItemOpen=new JMenuItem("Open");  //Open and Exit are the sub menu items under 'File' menu item  
     JMenuItem subMenuItemExit=new JMenuItem("Exit");  
     menuFile.add(subMenuItemOpen);  
     menuFile.add(subMenuItemExit);  
     menuBar.add(menuFile);  
     menuBar.add(menuEdit);  
     menuBar.add(menuView);  
     menuBar.add(menuHelp);  
     return menuBar;  
   }  
   public static void main(String[] args){  
     ToolBarExampleMain main=new ToolBarExampleMain();  
   }  
   public JPanel createToolBar(){  
     JToolBar toolBar=new JToolBar();  
     toolBar.setRollover(true);  
     //buttons required for the tool bar  
     JButton btnMoveUp;  
     JButton btnMoveDown;  
     JButton btnAdd;  
     JButton btnRemove;  
     JButton btnComment;  
     JButton btnMessageBox;  
     //icon which sets an image to each of the butons in the tool bar  
     Icon btnIcon;  
     //this JPanel will hold the tool bar  
     JPanel toolBarPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));  
     //Designing the Up Arrow button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\upArrow.png"));  
     btnMoveUp=new JButton(btnIcon);  
     //setting a tool tip for the button  
     btnMoveUp.setToolTipText("Move up ");  
     //adding the button to the toolbar  
     toolBar.add(btnMoveUp);  
     //adding a separator after the button  
     toolBar.addSeparator();  
     //Designing the Down Arrow button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\downArrow.png"));  
     btnMoveDown=new JButton(btnIcon);  
     btnMoveDown.setToolTipText("Move Down");  
     toolBar.add(btnMoveDown);  
     toolBar.addSeparator();  
     //Designing the Add button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\add.png"));  
     btnAdd=new JButton(btnIcon);  
     btnAdd.setToolTipText("Add new Test Case");  
     toolBar.add(btnAdd);  
     toolBar.addSeparator();  
     //Designing the Remove button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\remove.png"));  
     btnRemove=new JButton(btnIcon);  
     btnRemove.setToolTipText("Remove");  
     toolBar.add(btnRemove);  
     toolBar.addSeparator();  
     //Designing the Comment button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\comment.png"));  
     btnComment=new JButton(btnIcon);  
     btnComment.setToolTipText("Comment");  
     toolBar.add(btnComment);  
     toolBar.addSeparator();  
     //Designing the Message Box button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\messageBox.png"));  
     btnMessageBox=new JButton(btnIcon);  
     btnMessageBox.setToolTipText("Message Box");  
     toolBar.add(btnMessageBox);  
     toolBar.addSeparator();  
     toolBarPanel.add(toolBar);  
     return toolBarPanel;  
   }  
 }  
Note that you can add action listeners to the buttons in the tool bar as same as you do to JButtons.

Hope it helped you.