How to have th enter key repeat the action of a click of a button?

290 views Asked by At

I have a button that you can click that will do certain things, now I want the same actions however also when the enter button is pressed on the button(using the tab keys to get there and the enter key to press). This is essentially to create mouse free use. Below is the code for the button click. This Java in BlueJK and the scene is in scene builder however I am not using any code inside of scene builder. If someone could give me a lot of guidance and help that would be greatly appreciated.

@FXML   void newuserbackbuttonClicked()
{

    FXMLLoader loader = new FXMLLoader(Application.class.getResource("MainScene.fxml"));

    try
    {
        Stage stage2 = new Stage();
        stage2.setTitle("Main Scene");
        stage2.setScene(new Scene(loader.load()));
        stage2.show();           
        MainScene controller2 = loader.getController();
        controller2.prepareStageEvents(stage2);

    }
    catch (Exception ex)
    {
        System.out.println(ex.getMessage());
    }      
    stage.close();
}
1

There are 1 answers

2
tomsontom On

The important part is missing because you don't show the fxml. The right on* method you assign the controller method to is onAction. I guess you currently use onMouseClicked.