changeset 1143:1e32f8fd3b5d

RT-21454: ComboBox value should update when focus leaves it
author jgiles
date Thu, 24 May 2012 11:29:28 +1200
parents dc5dd86e13ab
children bad86696aadc
files javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ComboBoxListViewSkin.java
diffstat 1 files changed, 23 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ComboBoxListViewSkin.java	Thu May 24 10:47:39 2012 +1200
+++ b/javafx-ui-controls/src/com/sun/javafx/scene/control/skin/ComboBoxListViewSkin.java	Thu May 24 11:29:28 2012 +1200
@@ -101,10 +101,7 @@
                     KeyEvent ke = (KeyEvent)t;
                     
                     if (ke.getCode() == KeyCode.ENTER) {
-                        StringConverter<T> c = comboBox.getConverter();
-                        if (c == null) return;
-                        T value = c.fromString(textField.getText());
-                        comboBox.setValue(value);
+                        setTextFromTextFieldIntoComboBoxValue();
                         t.consume();
                         return;
                     } else if (ke.getCode() == KeyCode.F4 && ke.getEventType() == KeyEvent.KEY_RELEASED) {
@@ -236,11 +233,18 @@
         // focus always goes to the comboBox, which then forwards events down 
         // to the TextField. This ensures that the ComboBox appears focused
         // externally for people listening to the focus property.
+        // Also, (for RT-21454) set the currently typed text as the value when focus 
+        // is lost from the ComboBox
         textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
             @Override public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean hasFocus) {
                 if (hasFocus) {
                     comboBox.requestFocus();
                 }
+                
+                // RT-21454 starts here
+                if (! hasFocus) {
+                    setTextFromTextFieldIntoComboBoxValue();
+                }
             }
         });
 
@@ -287,6 +291,21 @@
         }
     }
     
+    private void setTextFromTextFieldIntoComboBoxValue() {
+        StringConverter<T> c = comboBox.getConverter();
+        if (c == null) return;
+        
+        T oldValue = comboBox.getValue();
+        T value = c.fromString(textField.getText());
+        
+        if ((value == null && oldValue == null) || (value != null && value.equals(oldValue))) {
+            // no point updating values needlessly (as they are the same)
+            return;
+        }
+        
+        comboBox.setValue(value);
+    }
+    
     private int getSelectedIndex() {
         T value = comboBox.getValue();
         int index = comboBox.getItems().indexOf(value);