How to create a Combo Box in Java

A combo box (or popup list) consists of a button that when clicked brings up a menu, and the user selects one item from this menu. This differs from a list in that before and after the menu selection is made only one item is visible. In Figure 1, the component dealing with shopping frequency labeled ''first time," "occasionally," and "frequently" is an example of a combo box. To create a combo box, first declare the combo box object to be of type JComboBox. For example,

JComboBox freqButton;



Then invoke the javax.swing.JComboBox constructor, as follows:

freqButton = new JComboBox (comboString) ;

where comboString is a String array containing the combo box items

String[] comboString = {"first time", "occasionally",
"frequently"};


By default a combo box is uneditable. A combo box can be made editable by using the javax.swing.JComboBox.setEditable method, for example

freqButton.setEditable(true);

No comments:

Post a Comment