Java Program to Create a Choice Menu to Select an Item

This is a Java program to create a choice menu to select an item and display it in the frame.

Problem Description

We have to write a program in Java such that it creates a choice menu consisting of 5 languages (C, C++, Java, Python, R), and the selected language from the choice menu is displayed in the frame.

Expected Input and Output

For displaying the language selected from a Choice Menu, we can have the following 5 sets of input and output.

1. Language selected ‘C’:

If the language C is selected from the Choice Menu (C, C++, Java, Python, R),
then the expected output is "Language Selected : C"

2. Language selected ‘C++’:

If the language C++ is selected from the Choice Menu (C, C++, Java, Python, R),
then the expected output is "Language Selected : C++"

3. Language selected ‘Java’:

advertisement
advertisement
If the language Java is selected from the Choice Menu (C, C++, Java, Python, R),
then the expected output is "Language Selected : Java"

4. Language selected ‘Python’:

If the language Python is selected from the Choice Menu (C, C++, Java, Python, R),
then the expected output is "Language Selected : Python"

5. Language selected ‘R’:

If the language R is selected from the Choice Menu (C, C++, Java, Python, R),
then the expected output is "Language Selected : R"
Problem Solution

1. Create a frame, a choice menu consisting of the 5 languages, and an output text field.
2. Add the choice menu to the frame.
3. Associate ItemListener with the choice menu, and display the item selected.

Program/Source Code

Here is source code of the Java Program to create a choice menu and display the item selected. The program is successfully compiled and tested using javac compiler on Fedora 30.

  1. /*Java Program to create a choice menu and display the item selected*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Choice_Menu implements ItemListener
  6. {
  7. 	static Choice menu;
  8. 	static JTextField text;
  9.         //Driver function
  10. 	public static void main(String args[])
  11. 	{
  12. 		//Create a frame
  13. 		JFrame frame=new JFrame("Choice Menu");
  14. 		frame.setSize(500,500);
  15. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. 		frame.setLayout(null);
  17. 		frame.getContentPane().setBackground(Color.white);
  18. 		//Create a choice menu
  19. 		menu=new Choice();
  20. 		menu.setBounds(100,100,120,60);
  21. 		frame.add(menu);
  22. 		//Add items to the menu
  23. 		menu.add("C");
  24. 		menu.add("C++");
  25. 		menu.add("Java");
  26. 		menu.add("Python");
  27. 		menu.add("R");
  28. 		//Create a text field for output
  29. 		text=new JTextField();
  30. 		text.setBounds(0,400,500,100);
  31. 		frame.add(text);
  32. 		//Create an object
  33. 		Choice_Menu obj=new Choice_Menu();
  34. 		//Associate ItemListener with the choice menu
  35. 		menu.addItemListener(obj);
  36. 		//Display the frame
  37. 		frame.setVisible(true);
  38. 	}
  39.         //function to display the item selected
  40. 	public void itemStateChanged(ItemEvent e)
  41. 	{
  42. 		String item=menu.getSelectedItem();
  43. 		text.setText("Language Selected : "+item);
  44. 	}
  45. }
Program Explanation

1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
3. Create a choice menu with a setBounds() values and add it to the frame by using frame.add();.
menu=new Choice(); –> Used to create the choice menu.
menu.setBounds(100,100,120,60); –> Sets the position of menu.
frame.add(menu); –> It will add the menu to the frame.

advertisement

4. Once the menu is created. Add the items to the menu by using menu.add(” “);. The menu items used in this program are:
menu.add(“C”); –> Item “C” added to the menu.
menu.add(“C++”); –> Item “C++” added to the menu.
menu.add(“Java”); –> Item “Java” added to the menu.
menu.add(“Python”); –> Item “Python” added to the menu.
menu.add(“R”); –> Item “R” added to the menu.

5. Create a text field with a setBounds() values and add it to the frame. This text field is used for output to display a selected item from the menu.
6. Choice_Menu obj=new Choice_Menu(); is used to create the object. Once object is created associate ItemListener with the choice menu.
7. frame.setVisible(true); is used to display the frame.
8. String item=menu.getSelectedItem();. Function getItemSelected returns the selected item in String format.
9. text.setText(“Language Selected : “+item); –> It displays the selected menu item from choice menu in the output textfield.

Runtime Test Cases

Here’s the run time test case for getting the item selected from a choice menu.

Test case 1 – Here’s the runtime output to test language ‘C’. For example, if the language C is selected from the Choice Menu (C, C++, Java, Python, R), then the program displays “Language Selected : C”.

advertisement

Test case 2 – Here’s the runtime output to test language ‘C++’. For example, if the language C++ is selected from the Choice Menu (C, C++, Java, Python, R), then the program displays “Language Selected : C++”.

Test case 3 – Here’s the runtime output to test language ‘Java’. For example, if the language Java is selected from the Choice Menu (C, C++, Java, Python, R), then the program displays “Language Selected : Java”.

Test case 4 – Here’s the runtime output to test language ‘Python’. For example, if the language Python is selected from the Choice Menu (C, C++, Java, Python, R), then the program displays “Language Selected : Python”.

Test case 5 – Here’s the runtime output to test language ‘R’. For example, if the language R is selected from the Choice Menu (C, C++, Java, Python, R), then the program displays “Language Selected : R”.

Sanfoundry Global Education & Learning Series – Java Programs.

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

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