JComboBox




The JComboBox class allows you to display a list of choices to choose from.
You must add an import statement:
import javax.swing.JComboBox;
JComboBox is a component or widget. You add components to a container, like a JPanel.

Examples:


String[] names = {"Tom", "Sally", "Bill", "Jane"};
JComboBox choices = new JComboBox(names);

choices.setSelectedIndex(2); // set Bill as the visible choice
// or
choices.setSelectedItem("Bill");


Other Common Methods:

choices.add("Juan");
choices.insert("Jill",0); // put Jill as the 1st element in the list
choices.remove("Sally");
choices.remove(2);

Note: You can also add an ItemListener or an ActionListener.