Java Program to Create a Menu with Several Menu Items

«
»

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.

advertisement

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.

advertisement
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.

  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.

advertisement

Test case 1 – To View the Menu.
java-program-menu-view

Test case 2 – To View the Menu Item Selected.
java-program-menu-item-3

Test case 3 – To View the Menu Item Selected.
java-program-menu-item-5

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn