Combo Boxes are typically used when the
developer wants the user to select only one of the options in a list and
the developer doesn't want the display to take up as much space ass it
would with radio button.. For
example, say you are filling out a form that requires you to enter the
state in which you live. You don't want to list out fifty radio
buttons. But you can put the states in a combo box that actually
takes only one line in a GUI, since it expands only when a user selects
Combo boxes, in Java, are constructed so that the user can only select one of them. At least in their JListBox variants, they can be configured to the user can make multiple selections. But, we will focus on single selection combo boxes.
Combo boxes, as they are implemented in Java, also allow for the user to type in their own input if their setEditable(true) property is set.
The following table contains a list of the constructors that are available for the JComboBox class.
|JComboBox( )||The constructor is used with no arguments|
|There are also a variety of methods that the developer will need to use in order to configure JComboBoxes aprropriately. Several of these methods are listed in the following table.|
|addItem(object)||Used to add objects to the combo box|
|setEditable(boolean)||Determines whether the user can type in their own input. If this property is not set, then the default is for requiring the user to select from the prescribed list.|
|getItem(int)||This returns the text of the list item at the position given by int. As with arrays the numbering starts at 0.|
|getItemCount( )||Returns the number of items in the list.|
|getSelectedIndex( )||Returns the index position of the currently selected item in the list.|
|getSelectedItem( )||Returns the text of the currently selected item.|
|setSelectedIndex(int)||This method selects the item at the position specified by the int.|
|setSelectedIndex(Object)||This method selects the specified Object in the list.|
|setMaximumRowCount(int)||This method sets the number of rows that are displayed in the combo box at one time.|
|While the constructor is much less than we have had
previously, we now have a much larger variety of built-in methods to work
Again, we will make use of ItemListeners when sensing user inputs relative to our combo boxes.
Displaying What's Selected. Now we present our first example, an application called SelectItem.java. This program allows the user to select an entry in a combo box which will cause a display to change.
public class SelectItem extends JFrame implements ItemListener
|The following image represents the GUI with the JComboBox selected and expanded.|
|The GUI itself, after the selection has been made, follows.|
|Notice how the words in the text box change as each
option in the combo box is selected.
The code will be discussed in class. Notice how I have put the ItemListener within the class for Select Item rather than developing its own class for handling the action.
Changing Icons. We are now going to use a program that I modified from Deitel and Deitel that changes an icon image based on item the user selects.
You need to copy each of the following images into the directory where you are running the program.
|You should call the application ComboBoxTest.java.|
public class ComboBoxTest extends JFrame
} // ends ComboBoxTest class
|The following image represents the GUI.|
|Notice how the displayed image changes based on the user selections.|