With an editable ComboBox
, is there any way to have the ENTER key event or action event handler occur regardless of whether or not the Combobox
's value property has changed?
I essentially would like to have the same behaviour in a ComboBox
's TextField
on pressing the ENTER key as it occurs for a TextField
.
What I Have Tried
My initial thought was to simply use setOnAction
for a ComboBox
; however, according to the documentation for it:
The ComboBox action, which is invoked whenever the ComboBox value property is changed. This may be due to the value property being programmatically changed, when the user selects an item in a popup list or dialog, or, in the case of editable ComboBoxes, it may be when the user provides their own input (be that via a TextField or some other input mechanism.
Thus, by using setOnAction
, the event handler only occurs if:
- The value property is changed via a change in selection from the drop down OR
- The value property is changed via user-input (ie: it does not occur if the user does not type anything and presses ENTER nor does it occur if the user does not change their input after the event handler has run once and they press ENTER).
Also, neither using setOnAction
on the ComboBox
's TextField
nor using setOnKeyPressed
achieves the desired behaviour.
Below is an SSCCE to demonstrate:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Example extends Application {
@Override
public void start(Stage primaryStage) {
ComboBox<String> comboBox =
new ComboBox<String>(
FXCollections.observableArrayList("XYZ", "ABC"));
comboBox.setEditable(true);
comboBox.setValue(comboBox.getValue());
comboBox.setOnAction((event) -> System.out
.println("occurs on selection changes or text changes and ENTER key"));
comboBox.getEditor().setOnAction(
(event) -> System.out.println("this never happens"));
comboBox.getEditor().setOnKeyPressed((keyEvent) -> {
if (keyEvent.getCode() == KeyCode.ENTER)
System.out.println("this never happens either");
});
TextField tf = new TextField();
tf.setOnAction((event) -> System.out.println("always happens on ENTER"));
HBox hbox = new HBox(comboBox, tf);
Scene scene = new Scene(hbox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}