-
Bug
-
Resolution: Duplicate
-
P4
-
7u6
As the test case below proves, combo box does not clear the selected item index when its item list is replaced.
In the test case below, I have too combo boxes. The first one is for choosing a category of either "Fruits" or "Cars". If you select the fruits category, you get a list of fruits in the second combo box to choose from. If you select a fruit, such as "Orange", the combo box of course sets the selected index to 0. If you then select the Cars category from the category combo box, then all of a sudden "Volvo" is selected. I am guessing this is caused by the selected item not being cleared.
Even adding a line to explicitly clear the selection (just remove the '//' in the code below) does not help.
In our application this bug is a serious problem, because when the user navigates through the item categories we have, it looks as if we favor certain products by automatically selecting them when a category is selected, and we cannot even work around this bug by explicitly clearing the selection. I would argue that ComboBox should always clear the selection if all items are replaced. It makes no sense, that users get random objects selected for them, just because they have made a previous selection.
public class SelectedItemBug extends Application {
private ComboBox<String> comboBoxA = new ComboBox();
private ComboBox<String> comboBoxB = new ComboBox();
@Override
public void start(Stage stage) throws Exception {
HBox hbox = new HBox();
hbox.getChildren().addAll(comboBoxA, comboBoxB);
comboBoxA.getItems().addAll("Fruits", "Cars");
comboBoxA.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String t, String newValue) {
if("Fruits".equals(newValue))
comboBoxB.getItems().setAll("Apple", "Orange");
else if("Cars".equals(newValue))
comboBoxB.getItems().setAll("Volkswagen", "Volvo");
//comboBoxB.getSelectionModel().clearSelection();
}
});
stage.setWidth(800);
stage.setScene(new Scene(hbox));
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
In the test case below, I have too combo boxes. The first one is for choosing a category of either "Fruits" or "Cars". If you select the fruits category, you get a list of fruits in the second combo box to choose from. If you select a fruit, such as "Orange", the combo box of course sets the selected index to 0. If you then select the Cars category from the category combo box, then all of a sudden "Volvo" is selected. I am guessing this is caused by the selected item not being cleared.
Even adding a line to explicitly clear the selection (just remove the '//' in the code below) does not help.
In our application this bug is a serious problem, because when the user navigates through the item categories we have, it looks as if we favor certain products by automatically selecting them when a category is selected, and we cannot even work around this bug by explicitly clearing the selection. I would argue that ComboBox should always clear the selection if all items are replaced. It makes no sense, that users get random objects selected for them, just because they have made a previous selection.
public class SelectedItemBug extends Application {
private ComboBox<String> comboBoxA = new ComboBox();
private ComboBox<String> comboBoxB = new ComboBox();
@Override
public void start(Stage stage) throws Exception {
HBox hbox = new HBox();
hbox.getChildren().addAll(comboBoxA, comboBoxB);
comboBoxA.getItems().addAll("Fruits", "Cars");
comboBoxA.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String t, String newValue) {
if("Fruits".equals(newValue))
comboBoxB.getItems().setAll("Apple", "Orange");
else if("Cars".equals(newValue))
comboBoxB.getItems().setAll("Volkswagen", "Volvo");
//comboBoxB.getSelectionModel().clearSelection();
}
});
stage.setWidth(800);
stage.setScene(new Scene(hbox));
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}