0
votes

I am developing a data entry GUI that has ~12 textfields the user enters data into, and then a "Save" button is clicked with the mouse to save that data.

I added a KeyEvent to make it so when the user presses "Enter", it also saves the data:

//Button Shortcut - Enter/Save
@FXML
public void handleEnterPressed(KeyEvent event) {
    if (event.getCode() == KeyCode.ENTER) {
        saveButton.requestFocus();
        newCard(null);
        event.consume();            
    }
}

This works - IF the save button is focused. For example, I click the save button, it saves the data. I press "Enter" - it saves the data again. If I tab through all the fields until the focus is back to the save button, it will save. But if, for example, my cursor is on one of the textfields and I hit "Enter" nothing happens. (newCard is the method that saves the textfield data to a list).

I made a new button in my controller:

 @FXML private Button saveButton;

set the fx:id in Scene Builder to it and tried to use the .requestFocus(), but it doesn't seem to work. But it doesn't throw an error either so I'm not sure if I'm just using it incorrectly.

1
Why do you have to go through the Save button? Why not call the save() method directly from the handler for your KeyEvent? - MarsAtomic
Ok so I wasn't quite sure what you meant by the "handler for your KeyEvent", I'm pretty sure it's already doing that. BUT, it got me thinking why I have the KeyEvent bound to the button itself in SceneBuilder...and if I could bind it somewhere else? And it turns out, I can bind it to the AnchorPane! So now you can hit "Enter" wherever you want and it will save the data. Thanks for making me think about it. - erik-sn

1 Answers

0
votes

MarsAtomic's suggestion made me think in a different direction. I had bound the KeyEvent to the Save button. However, once I bound the button to the AnchorPane itself it started working perfectly.