The example below registers an actionHandler to a textField/comboBox which compares the text of the field/editor with the value of the formatter/combo at the time of receiving the actionEvent. Run and
- edit textField, press enter
- expected: textField.text == formatter.value
- actual: formatter.value is oldValue
- edit combo, press enter
- expected and actual: editor.text == combo.value
The example:
public class TextFieldAction extends Application {
private Parent getContent() {
// ComboBox behaves as expected
ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "All");
// ComboBoxX<String> comboBox = new ComboBoxX<>(items);
ComboBox<String> comboBox = new ComboBox<String>(items);
comboBox.setEditable(true);
comboBox.setValue(items.get(0));
comboBox.setOnAction(e -> {
System.out.println("combo action: " +
comboBox.getEditor().getText() + "=" + comboBox.getValue());
});
// compare to TextField with TextFormatter: value not yet committed
TextField textField = new TextField();
TextFormatter<String> formatter = new TextFormatter<>(IDENTITY_STRING_CONVERTER, "initial");
textField.setTextFormatter(formatter);
textField.setOnAction(e -> {
System.out.println("textfield action: " +
textField.getText() + "=" + formatter.getValue());
});
// compare Spinner
Spinner spinner = new Spinner();
// normal setup of spinner
SpinnerValueFactory factory = new IntegerSpinnerValueFactory(0, 10000, 0);
spinner.setValueFactory(factory);
spinner.setEditable(true);
// spinner has no action (should it?
// spinner.setOnAction(e -> {
// using action of editor
spinner.getEditor().setOnAction(e -> {
System.out.println("spinner-editor action: " +
spinner.getEditor().getText() + "=" + spinner.getValue());
});
VBox box = new VBox(10, textField, comboBox, spinner);
return box;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(getContent()));
primaryStage.setTitle(FXUtils.version());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
- edit textField, press enter
- expected: textField.text == formatter.value
- actual: formatter.value is oldValue
- edit combo, press enter
- expected and actual: editor.text == combo.value
The example:
public class TextFieldAction extends Application {
private Parent getContent() {
// ComboBox behaves as expected
ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "All");
// ComboBoxX<String> comboBox = new ComboBoxX<>(items);
ComboBox<String> comboBox = new ComboBox<String>(items);
comboBox.setEditable(true);
comboBox.setValue(items.get(0));
comboBox.setOnAction(e -> {
System.out.println("combo action: " +
comboBox.getEditor().getText() + "=" + comboBox.getValue());
});
// compare to TextField with TextFormatter: value not yet committed
TextField textField = new TextField();
TextFormatter<String> formatter = new TextFormatter<>(IDENTITY_STRING_CONVERTER, "initial");
textField.setTextFormatter(formatter);
textField.setOnAction(e -> {
System.out.println("textfield action: " +
textField.getText() + "=" + formatter.getValue());
});
// compare Spinner
Spinner spinner = new Spinner();
// normal setup of spinner
SpinnerValueFactory factory = new IntegerSpinnerValueFactory(0, 10000, 0);
spinner.setValueFactory(factory);
spinner.setEditable(true);
// spinner has no action (should it?
// spinner.setOnAction(e -> {
// using action of editor
spinner.getEditor().setOnAction(e -> {
System.out.println("spinner-editor action: " +
spinner.getEditor().getText() + "=" + spinner.getValue());
});
VBox box = new VBox(10, textField, comboBox, spinner);
return box;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(getContent()));
primaryStage.setTitle(FXUtils.version());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}