Java Program to Create a Combo Box

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.

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.

  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.

Test case 2 – To view the label of item selected.


Test case 3 – To view the label of 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.