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
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-
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
Output on Command Prompt
Output Displayed as