This is a Java program to create a choice menu to select an item and display it in the frame.
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.
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’:
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"
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.
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.
/*Java Program to create a choice menu and display the item selected*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Choice_Menu implements ItemListener
{
static Choice menu;
static JTextField text;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("Choice Menu");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.white);
//Create a choice menu
menu=new Choice();
menu.setBounds(100,100,120,60);
frame.add(menu);
//Add items to the menu
menu.add("C");
menu.add("C++");
menu.add("Java");
menu.add("Python");
menu.add("R");
//Create a text field for output
text=new JTextField();
text.setBounds(0,400,500,100);
frame.add(text);
//Create an object
Choice_Menu obj=new Choice_Menu();
//Associate ItemListener with the choice menu
menu.addItemListener(obj);
//Display the frame
frame.setVisible(true);
}
//function to display the item selected
public void itemStateChanged(ItemEvent e)
{
String item=menu.getSelectedItem();
text.setText("Language Selected : "+item);
}
}
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.
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.
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”.
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]- Apply for Java Internship
- Practice BCA MCQs
- Check Java Books
- Practice Programming MCQs
- Apply for Computer Science Internship