I have a javafx editable ComboBox.
The value property is updated only when i press enter, and not just exiting the ComboBox.
It seems to me to be a bug and not a design feature because it would be strange that a non focused control shows a value that is not reflected by its value property.
Here is an SSCE that shows the problem:
import javafx.application.Application;
import javafx.beans.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class T02 extends Application {
public static void main (String [] args) { launch(args); }
@Override public void start(Stage stage) throws Exception {
System.out.println("javafx.runtime.version: " + System.getProperties().get("javafx.runtime.version"));
VBox vbox = new VBox();
//this combobox is the object of the investigation
final ComboBox comboBox = new ComboBox();
comboBox.setEditable(true);
vbox.getChildren().add(comboBox);
//I need another component just to allow the ComboBox to loose the focus
TextField textField = new TextField();
vbox.getChildren().add(textField);
//And here it is: when comboBox looses focus, i print its state
comboBox.focusedProperty().addListener(new InvalidationListener() {
@Override public void invalidated(Observable observable) {
//return if it has the focus: i am just interested on focus lost
if (comboBox.focusedProperty().get()) {return;}
System.out.println(comboBox.getValue());
}
} );
stage.setScene(new Scene(vbox));
stage.show();
}
}
output:
[write something on combo and click on the other control (or press tab, it’s the same)]
null
[click back on the combo, press enter and click down on the text field]
hello
The first null is strange, maybe buggy as I said before. I did a quick search on jira and haven’t found this behavior. (Maybe i haven’t noticed it)
update
Ok, i’ve just added the
System.out.println("javafx.runtime.version: " + System.getProperties().get("javafx.runtime.version"));
and get the result:
javafx.runtime.version: 2.1.something
I’ve upgraded the version with the last available:
javafx.runtime.version: 2.2.3-b05
And the issue disappears.
The related bug which was fixed in RT-21454 ComboBox value should update when focus leaves it.
Update
AgostinoX reports that the issue is resolved by upgrading to
JavaFX 2.2.3-b05.You can check the javafx version you are using by placing the following code inside your JavaFX application’s start method.