Java Program to Create a Combo Box to Select One Option and Display the Label of Option Selected

«
»

This is a Java Program to Create a Combo Box to Select One Option and Display the Label of Option Selected

Problem Description

We have to write a program in Java such that it creates a Combo Box containing name of some countries, and the option selected by user is displayed in the frame.

Expected Input and Output

For displaying label of option selected from a Combo Box, we can have the following set of input and output.

advertisement

To display the label:

When any option on the Combo Box is selected by the user,
it is expected that the label of the item selected is displayed.
Problem Solution

1. Create a combo box using the JComboBox class.
2. Add the list of countries to it.
3. Associate ItemListener with the combo box.
4. When any item is selected, display the label of item in the frame.

Program/Source Code

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

advertisement
  1. /* Java Program to view label of item selected from a Combo Box*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Combo_Box implements ItemListener
  6. {
  7.     static JLabel text;
  8.     static JComboBox<String> box; 
  9.     //Driver function
  10.     public static void main(String args[])
  11.     {
  12. 	//Create a frame
  13. 	JFrame frame = new JFrame("Combo Box");
  14. 	frame.setSize(500,500);
  15. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. 	frame.setLayout(null);
  17. 	//Create an object
  18. 	Combo_Box obj = new Combo_Box();
  19. 	//Create a label	
  20. 	text = new JLabel("Select an Item");
  21. 	text.setBounds(175,50,200,50);
  22. 	frame.add(text);
  23. 	//Create an array with countries name
  24. 	String countries[]={"India","Japan","Nepal","China","Sri Lanka"};
  25. 	//Create a Combo Box
  26. 	box = new JComboBox<String>(countries);
  27. 	box.setBounds(200,200,100,50);
  28. 	box.addItemListener(obj);
  29. 	frame.add(box);
  30. 	//Display the frame
  31. 	frame.setVisible(true);
  32.     }
  33.     //Function to view the label of item selected
  34.     public void itemStateChanged(ItemEvent e)
  35.     {
  36. 	text.setText("Item Selected : "+box.getSelectedItem());
  37.     }
  38. }
Program Explanation

1. Create a Combo Box using JComboBox, with String data type.
2. To know which item was selected, using getSelectedItem.

Runtime Test Cases

Here’s the run time test cases for viewing the label of item selected from a combo box.

Test case 1 – To view the label of item selected.
java-program-combo-box-1

Test case 2 – To view the label of item selected.
java-program-combo-box-2

advertisement

Test case 3 – To view the label of item selected.
java-program-combo-box-3

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