0
votes

In my code I have bound the defaultProperty of a button to some other properties like so:

BooleanBinding isAddResourceButtonDefault
        = resourceNameTextField.focusedProperty().or(
                resourceTypeComboBox.focusedProperty()).or(
                divisibleResourceCheckbox.focusedProperty().or(
                maxInstancesTextField.focusedProperty()).or(
                addResourceButton.focusedProperty()));
addResourceButton.defaultButtonProperty().bind(isAddResourceButtonDefault);

The problem is that the addResourceButton won't fire absolutely any events on pushing Enter - neither OnAction, nor OnKeyPressed.

The default property gets set just fine - I double checked (using output and ScenicView). ScenicView also doesn't show any events being fired upon hitting keys. Any ideas?

1

1 Answers

1
votes

I believe TextFields (and other TextInputControls) process and consume key events that occur on them. So pressing Enter on a text field will not fire an action event on the default button (because the key event never reaches the Scene).

If you are just trying to fire the button's action event handler when enter is pressed in one of the text fields, just add the same event handler to the text fields. E.g.

EventHandler<ActionEvent> addResourceHandler = event -> {
    System.out.println("Add resource");
    // ...
};

addResourceButton.setOnAction(addResourceHandler);
resourceNameTextField.setOnAction(addResourceHandler);
maxInstancesTextField.setOnAction(addResourceHandler);