changeset 5852:24f2584b90eb

RT-34573: Resetting ComboBox items makes the buttonCell display the invalid last selected item
author jgiles
date Fri, 29 Nov 2013 08:16:34 +1300
parents 24dda4be6617
children 20724ec5e62e
files modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ComboBoxListViewSkin.java modules/controls/src/test/java/javafx/scene/control/ComboBoxTest.java
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ComboBoxListViewSkin.java	Thu Nov 28 15:22:57 2013 +1300
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ComboBoxListViewSkin.java	Fri Nov 29 08:16:34 2013 +1300
@@ -452,6 +452,7 @@
             } else {
                 // RT-21336 Show the ComboBox value even though it doesn't
                 // exist in the ComboBox items list (part two of fix)
+                buttonCell.updateIndex(-1);
                 boolean empty = updateDisplayText(buttonCell, value, false);
                 
                 // Note that empty boolean collected above. This is used to resolve
--- a/modules/controls/src/test/java/javafx/scene/control/ComboBoxTest.java	Thu Nov 28 15:22:57 2013 +1300
+++ b/modules/controls/src/test/java/javafx/scene/control/ComboBoxTest.java	Fri Nov 29 08:16:34 2013 +1300
@@ -1102,4 +1102,27 @@
         assertNull(comboBox.getTooltip());
         assertNull(comboBox.getEditor().getTooltip());
     }
+
+    @Test public void test_rt34573() {
+        final ComboBox<String> comboBox = new ComboBox<>();
+
+        final ListCell<String> customCell = new ListCell<String>() {
+            @Override protected void updateItem(String item, boolean empty) {
+                super.updateItem(item, empty);
+                setText(item);
+            }
+        };
+        comboBox.setButtonCell(customCell);
+
+        new StageLoader(comboBox);
+
+        comboBox.setItems(FXCollections.observableArrayList("A","B","C","D"));
+        comboBox.setValue("B");
+        assertEquals("B", comboBox.getButtonCell().getText());
+        assertEquals(1, comboBox.getButtonCell().getIndex());
+
+        comboBox.setItems(FXCollections.observableArrayList("1","2","3","4"));
+        assertTrue(comboBox.getButtonCell().getText().isEmpty());
+        assertEquals(-1, comboBox.getButtonCell().getIndex());
+    }
 }