I’ve observed that editable ComboBox
value not updated when default button is used. I mean that if user enters a new value in ComboBox
and presses enter then he won’t get the value he has entered. If he clicks OK button using mouse then everything is OK. This my code:
public class JavaFxTest7 extends Application {
@Override
public void start(Stage stage) {
var comboBox = new ComboBox<String>();
comboBox.setEditable(true);
var button = new Button("OK");
button.setDefaultButton(true);
button.setOnAction((e) -> {
System.out.println("Value: " + comboBox.valueProperty().get());
});
var vBox = new VBox(comboBox, button);
Scene scene = new Scene(vBox, 400, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
If you run it and enter something in ComboBox
and press enter you will see this output:
Value: null
I could fix it using this code:
button.setOnAction((e) -> {
button.requestFocus();//<---- added
System.out.println("Value: " + comboBox.valueProperty().get());
});
But it looks strange. Could anyone say how to fix this problem or maybe I am doing something wrong.