JComboBox Set Selected Item In Java Example
Chapter:
Swing
Last Updated:
08-07-2016 00:53:33 UTC
Program:
/* ............... START ............... */
import javax.swing.JComboBox;
public class JavaJComboBoxSetSelectedItem {
public static void main(String[] argv) throws Exception {
String[] items = { "item1", "item2" };
JComboBox comboBox = new JComboBox(items);
// Get current value
Object object = comboBox.getSelectedItem();
// Set a new value
comboBox.setSelectedItem("item2");
object = comboBox.getSelectedItem();
// If the new value is not in the list of valid items, the call is ignored
comboBox.setSelectedItem("item3");
object = comboBox.getSelectedItem();
}
}
/* ............... END ............... */
Tags
JComboBox Set Selected Item, Java, Swing