Return to Start Menu when pressing ESC with Javafx

Asked

Viewed 1,058 times

3

How can I get my application back to the start menu after pressing ESC. I can currently use Keyevent only when some component is associated, such as a TextField, Button, etc.. I just want to press the Esc without any components being selected and the application goes back to the main menu. Example of how I do (Associating a component):

txtPesquisar.setOnKeyPressed(k -> {
            final KeyCombination ENTER = new KeyCodeCombination(KeyCode.ENTER);
            if (ENTER.match(k)) {
                atualizar();
            }
        });
  • What is your main menu? You have multiple screens?

  • Yes, I have several screens. My main menu is just a "welcome screen"

  • Okay, but you already know how to change the screens, right? It would just be the case to do it through a "shortcut key"?

  • Yes, that’s right. When I press a key changes Scene. Scene is instantiated in my Main method that way: Parent root = FXMLLoader.load(getClass().getResource("frmLogin.fxml"));
 Scene scene = new Scene(root);

  • Then I take the scene and play in a static variable. And with this variable I manipulate all transitions of scenes from the system

1 answer

1


Just add a Handler to your set.

scene.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent t) -> {
    if (t.getCode() == KeyCode.ESCAPE) {
        //codigo para ir ao menu inicial
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.