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


How to convert String Date to Timestamp in Java? Best Java Tutorial for Beginners Read user input in Java using Scanner Java Programming Examples Read file line by line in Java 8 What are the benefits of learning Java Programming? A brief history and future of Java programming language Importance of Java for Mobile Application Development How To Become A Good Software Developer? Learn Java Programming for beginners 5 Reasons Java is Still a Great Language to Boost Your Career Prospect as a Programmer How to learn Java with no programming experience? How to learn Java? What are Classes and Interfaces in Java? Converting a String to Integer in Java How to check if a string exists in a string in JavaScript? Installing JDK 8 on Windows 10 and configuring the settings Video Tutorial of reading file in Java 9 Invincible Reasons to learn Java New to Java programming Java Programming Tips, Articles and Notes How to download JDK 1.6? Encapsulation in Java Programming How to format number in Java? Generics Example Program in Java Java 9 Features Java 9 Tutorial, news and examples How to read content of JPEG file in Java? Image Processing Tutorials in Java Programming Language Introduction to Java Technology Java 8 Consumer Interface with forEach Loop Introduction to Generics in Java Java 8 consumer class(interface) example Oracle Launches Project Valhalla for Java How to read properties file in Java? How to add JDK 8 support in Eclipse? Java 8: Java 8 is officially released and it can be downloaded How to Invoke method using Reflection API of Java? First Lambda Expressions in Java Java 8 expected release date

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()

   setSize(200, 350);

   JMenuBar menuBar = new JMenuBar();


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

  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");

   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();

Output on Command Prompt


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

Output Displayed as 



Share on Google+Share on Google+

Menu Bar in Java

Posted on: April 17, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Menu Bar in Java  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 9, 2011

Is it possible to create menu in jsp ? If yes then please suggest a solution for this problem... With great regards..
narendra gupta
September 9, 2011
Java related help

Is it possible to create menu bar in jsp? if possible then please suggest a solution ... With great regards
September 24, 2011
About the menubar program

Three errors are there in the menubar program.