Page 762 - Introduction to Programming with Java: A Problem Solving Approach
P. 762

                728
Chapter 17 GUI Programming—Component Layout, Additional GUI Components
public void setSelectedIndex(int index)
Changes the currently selected item to the item at the given index position.
public void addActionListener(ActionListener listener)
Adds a listener to the combo box.
The setVisible and addActionListener methods should look familiar by now. The other methods are new and require further explanation. Let’s start with setEditable. If a combo box calls setEditable(true), the combo box’s top portion becomes editable. That means that a user can enter text into it the same as if it were a text box component. Additionally, the user still can use the pull-down portion of the combo box the same as always. Combo boxes are named “combo” for “combination” because they are capable of implementing a mixture of components—part pull-down list, part text box. But most programmers don’t bother with the combo box’s text-box capability. They usually stick with the default be- havior, where the top portion of the combo box is not editable.
The getSelectedItem method returns the currently selected item. For example, here’s how you can retrieve the currently selected daysBox item and store it in a favoriteDay variable:
String favoriteDay = (String) daysBox.getSelectedItem();
What’s the point of the (String) cast operator? The getSelectedItem method is defined to have a return type of Object. Therefore, if there were no cast operator, the compiler would see an Object at the right being assigned into a String at the left, and that would generate a compile-time error. But there is a cast operator, so the compiler sees a String at the right being assigned into a String at the left. And that makes the compiler happy.
Apago PDF Enhancer
If you’d like to programmatically select an option from a combo box, call setSelectedItem and pass in the item that you want to select. For example, to select Friday from the daysBox component, do this:
daysBox.setSelectedItem("Friday");
Normally, you’ll call setSelectedItem with an argument that matches one of the combo box’s items. But that’s not always the case. If you’d like to clear a combo box so that no options are selected, call setSelectedItem(null). If you call setSelectedItem with a different item (not null and not a combo box item), then nothing happens. Well, actually, nothing happens if it’s a standard combo box. But if it’s an editable combo box, then the passed-in item gets put into the editable top portion of the combo box.
There are two ways to access items in a combo box—use item names or use item indexes. The getSelectedItem and setSelectedItem methods use item names. The getSelectedIndex and setSelectedIndex methods use item indexes. For example, note how this code fragment calls setSelectedIndex with an index value of 2:
String[] days =
{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
daysBox = new JComboBox(days);
daysBox.setSelectedIndex(2);
Since combo boxes store their items in arrays, combo box item indexes are 0-based. Therefore, in the above code fragment, Monday is 0, Tuesday is 1, and Wednesday is 2. Thus, daysBox.setSelectedIndex(2)
changes the selected item to Wednesday.
Now for a short brain-teaser. Given the above code fragment, how can you change the cur-
rently selected day to the next day? Do arithmetic with the index, like this:
 Indexing helps you process data.
 daysBox.setSelectedIndex(daysBox.getSelectedIndex() + 1);










































































   760   761   762   763   764