Java Program to Create a Menu and Menu Bar

This is a Java Program to Create a Menu with Several Menu Items

Problem Description

We have to write a program in Java such that it creates a menu bar with menu items and the the label of the menu item is displayed in the frame.

Expected Input and Output

For creating a menu with several items, we can have the following sets of input and output.

1.To View the Menu:

When the program is being executed,
it is expected that the frame consists of the menu.

2.To Select a Menu Item:

When any menu item is selected from the menu bar,
it is expected that the label of the menu item selected is displayed.
Problem Solution

1. Create a menu and add menu items to it.
2. Add ActionListener to all menu items.
3. Create a menu bar and add the menu to it.
4. Create a label and display the menu item selected.

Program/Source Code

Here is source code of the Java Program to view the label of item selected from a Menu. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. /*Java Program to Create a Menu and Display the Menu Item Selected*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Menu implements ActionListener
  6. {
  7.     static JLabel text;
  8.     //Driver function
  9.     public static void main(String args[])
  10.     {
  11. 	//Create a frame
  12. 	JFrame frame = new JFrame("Menu");
  13. 	frame.setSize(500,500);
  14. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 	frame.setLayout(new FlowLayout());
  16. 	//Create an object
  17. 	Menu obj = new Menu();
  18. 	//Create a Menu
  19. 	JMenu menu = new JMenu("Select Here");
  20. 	//Create Menu Items
  21. 	JMenuItem item[] = new JMenuItem[5];
  22. 	for(int i=0;i<5;i++)
  23. 	{
  24. 	    item[i]=new JMenuItem("Item "+(i+1));
  25. 	    item[i].addActionListener(obj);
  26. 	    menu.add(item[i]);
  27. 	}
  28. 	//Create a menu bar
  29. 	JMenuBar mb=new JMenuBar();
  30. 	mb.add(menu);
  31. 	frame.setJMenuBar(mb);
  32. 	//Create the label
  33. 	text = new JLabel();
  34. 	frame.add(text);
  35. 	//Display the frame
  36. 	frame.setVisible(true);
  37.     }
  38.     //Function to display the menu item selected
  39.     public void actionPerformed(ActionEvent e)
  40.     {
  41. 	text.setText("Menu Item Selected : "+e.getActionCommand());
  42.     }
  43. }
Program Explanation

1. Create a menu using JMenu class.
2. To create menu items use the JMenuItem class.
3. Create a menu bar using JMenuBar and add the menu to it.
4. Set the menu to the frame using setJMenuBar function.
5. Display the label of the menu item selected.

Runtime Test Cases

Here’s the run time test cases for selecting an item from a menu.


Test case 1 – To View the Menu.

Test case 2 – To View the Menu Item Selected.

Test case 3 – To View the Menu Item Selected.


Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.