[an error occurred while processing this directive]

Menu Bar in Java

                         

A Menu Bar is a set of option that allow the user to choose from any one of the  saving option. A list of other option component  a user can choose in a menu bar. In this Tutorial we want to describe you how to create a Menu Bar in your application, First we need to import list of packages that required for creating a menu bar in an application. We defined a class name 'MenuBarExample' extends JFrame.A JFrame is a window working in a swing environment. We define a constructor that is used to create  a JFrame with a specified size of 200*350.The java  code also describe you to exit when the JFrame when it is closed and show us the window. The JMenuBar is used to control the list of bar at the top of window, this includes File,Edit,View,etc.JMenuBar is positioned at the menubar in the JFrame.The MenuBar is set at the JMenuBar.We define and add three dropdown menu in the menubar i.e

  1. File
  2. Edit
  3. View

After we have done with the list of option in menuBar,then we go for the sub menuBar options i.e. we create and add simple menu item to the list of dropdown menu present in menuBar.The various submenu that we have used in this menu Bar are-

File Edit View
New Copy  Toolbars
Open Paste -
Exit Cut -

The class implement Action Listener, that is used to add  action listener to submenu item. Action listener is a set of event  generated when a user perform certain action on a component.Example,when a user click or perform a certain operation on button and textfield,as a result a certain action event is generated on the clicking of a button or enter in a text field. The result come out is action performed that is given to all action listener registered with respective component. 

Let us go through list of method  to understand  example program code

1.newAction.addActionListener(new Action Listener())-This is used to register an instance of the event handler class as a listener on  components

2.public void actionPerformed(ActionEvent arg0)        - The program must register this object as an action listener the component source, using the addActionListener method. When the user clicks the onscreen component, the component fires an action event. This results in the invocation of the action listener's action Performed method. The single argument to the method is an Action Event object that gives information about the event and its source. The System.out.println is used to display the "you have clicked on the open action" as long as you clicked on open submenu.

Code for Menu Bar

    

     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.JFrame;
     import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    public class MenuBarExample extends JFrame
{
       public MenuBarExample()
{

       setTitle("MenuBarExample");
       setSize(200, 350);


       JMenuBar menuBar = new JMenuBar();

      setJMenuBar(menuBar);


      JMenu fileMenu = new JMenu("File");
      JMenu editMenu = new JMenu("Edit");
      JMenu viewMenu = new JMenu("View");
      menuBar.add(fileMenu);
      menuBar.add(editMenu);
      menuBar.add(viewMenu);


      JMenuItem newAction =   new JMenuItem("New");
      JMenuItem openAction =  new JMenuItem("Open");
      JMenuItem exitAction =    new JMenuItem("Exit");
      JMenuItem cutAction =     new JMenuItem("Cut");
      JMenuItem copyAction =  new  JMenuItem("Copy");
      JMenuItem pasteAction =  new JMenuItem("Paste");
      JMenuItem toolbarsAction= new JMenuItem("toolbarsAction");
      fileMenu.add(newAction);
      fileMenu.add(openAction);
      fileMenu.addSeparator();
      fileMenu.add(exitAction);
      editMenu.add(cutAction);
      editMenu.add(copyAction);
      editMenu.add(pasteAction);
      editMenu.addSeparator();
      viewMenu.add(toolbarsAction);

       newAction.addActionListener(new ActionListener();

 {
      public void actionPerformed(ActionEvent arg0)

 {
      System.out.println("You clicked on the new action");
  } 

  }

  };

  }

     public static void main(String[] args) 

 {
     MenuBarExample mba = new MenuBarExample();
     mba.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     mba.setVisible(true);
 }
 }

Output on Command Prompt


C:\saurabh>javac MenuBarExample.java

C:\saurabh>java MenuBarExample
You  clicked on the new action
You  clicked on the new action

Output Displayed as 

 


                         

[an error occurred while processing this directive]