changeset 5162:8db542421315

RT-29433 ChoiceBox keeps selected item when underlying collection is replaced. unit test : checkSelectedItemAfterReplacingDataWithEmptyList()
author psomashe
date Thu, 19 Sep 2013 09:42:26 -0700
parents 5031afa0b69d
children 2441235395a4
files modules/controls/src/main/java/javafx/scene/control/ChoiceBox.java modules/controls/src/test/java/javafx/scene/control/ChoiceBoxTest.java
diffstat 2 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/control/ChoiceBox.java	Thu Sep 19 15:56:38 2013 +0400
+++ b/modules/controls/src/main/java/javafx/scene/control/ChoiceBox.java	Thu Sep 19 09:42:26 2013 -0700
@@ -195,7 +195,8 @@
                 final SingleSelectionModel<T> sm = getSelectionModel();
                 if (sm != null) {
                     if (newItems != null && newItems.isEmpty()) {
-                        sm.setSelectedIndex(-1);
+                        // RT-29433 - clear selection.
+                        sm.clearSelection();
                     } else if (sm.getSelectedIndex() == -1 && sm.getSelectedItem() != null) {
                         int newIndex = getItems().indexOf(sm.getSelectedItem());
                         if (newIndex != -1) {
--- a/modules/controls/src/test/java/javafx/scene/control/ChoiceBoxTest.java	Thu Sep 19 15:56:38 2013 +0400
+++ b/modules/controls/src/test/java/javafx/scene/control/ChoiceBoxTest.java	Thu Sep 19 09:42:26 2013 -0700
@@ -459,4 +459,15 @@
         assertEquals("Apple", ChoiceBoxSkinNodesRetriever.getChoiceBoxSelectedText(skin));
         
     }
+    
+    @Test public void checkSelectedItemAfterReplacingDataWithEmptyList() {
+        StackPane pane = new StackPane();
+        pane.getChildren().add(box);
+        box.getItems().addAll("Apple", "Orange", "Banana");
+        box.getSelectionModel().select("Orange");
+        startApp(pane);
+        box.getItems().clear();
+        // make sure the selected item is null
+        assertEquals(null, box.getSelectionModel().getSelectedItem());
+    }
 }