Java Reference
In-Depth Information
FIGURE 9.5
The FormatFrame
application.
Choosing one of the radio buttons causes the existing choice to be deselected.
Combo Boxes
The Swing class JComboBox can be used to create combo boxes , components that present
a drop-down menu from which a single value can be selected. By hiding the menu when
the component is not being used, it takes up less space in a graphical user interface.
The following steps show how a combo box can be created:
1. The JComboBox() constructor is used with no arguments.
2. The combo box's addItem( Object ) method adds items to the list.
In a combo box, users will be able to select only one of the items in the drop-down
menu. If the component's setEditable() method is called with true as an argument, it
also supports the entry of text. This feature gives combo boxes their name—a component
configured in this manner serves as both a drop-down menu and a text field.
The JComboBox class has several methods that can be used to control a drop-down list or
combo box:
getItemAt( int ) —Return the text of the list item at the index position specified by
the integer argument. As with arrays, the first item of a choice list is at index posi-
tion 0, the second at position 1, and so on.
n
getItemCount() —Return the number of items in the list.
n
getSelectedIndex() —Return the index position of the currently selected item in
the list.
n
getSelectedItem() —Return the text of the currently selected item.
n
setSelectedIndex( int ) —Select the item at the indicated index position.
n
setSelectedIndex( Object ) —Select the specified object in the list.
n
The FormatFrame2 application in Listing 9.7 contains an application that rewrites the
preceding radio button example. The program uses an uneditable combo box to choose
one of four options.
Search WWH ::




Custom Search