Problem with Keypressed Event to open another JAVAFX window

Asked

Viewed 298 times

2

My problem is the following, this way that I am opening the window I have a problem about using the Keypressed function.Because this way my method needs the Actionevent parameter that when using the getSource() method, returns me the button and opens the window correctly, but that when I click the button.Then for the button function there to work inside the keyPressed function, I would have to put a parameter there too. I tried to put new Actionevent and(), the error goes away but the source is different when I press enter, which causes the window n to be opened.

Below is the example that opens the second window by clicking on the button. And last the Keypressed event.

Aah, the idea is a login window, then when I give enter do the checks and Talz and open the other window.But only works by clicking even

private void openJanela(Actionevent Event) throws Exception {

    Parent main_tela = FXMLLoader.load(getClass().getResource("FXMLTelaPrincipal.fxml"));
    Scene main = new Scene(main_tela);
    Stage st = (Stage) ((Node) event.getSource()).getScene().getWindow();
    st.hide();
    st.setScene(main);
    st.show();

}

public void initialize(URL url, ResourceBundle rb) {
    btnLogar.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent event) -> {
        try {
            if (event.getCode() == KeyCode.ENTER) {
                abrirJanela();
            }
        } catch (Exception ex) {
        }
    });

1 answer

0


Javafx has an easier way to make a button react to the ENTER event, just put it default button. Basically a button in Javafx can be 3 types:

  • Normal: Default behavior, react only with click;
  • Default: A default button is a button that also triggers with a VK_ENTER type keyboard event, provided that no other node on the scene consumes that event;
  • Cancel: A cancel button is a button that also triggers with a VK_ESC type keyboard event, provided that no other node on the scene consumes this event;

See the correct way below:

Button mybutton = new Button();
mybutton.setDefaultButton(true);

mybutton.setOnAction((ActionEvent t) -> {
    // Seu evento
});
  • Obg Brother, you solved my problem. But could you explain to me why actionEvent in this way works and when I create one like Actionevent ae = new Actionevent() Doesn’t it work? could you explain to me??

  • Because Actionevent is a type of event, not an event capturer. setOnAction receives an Eventhandler<Actionevent>. In the way I wrote I made use of Amblas, in the traditional form would be setOnAction(new Eventhandler<Actionevent>() { ... }

  • I get it, thank you. It worked the way you said, but I can trigger this event with SPACE tb, and the idea was to limit only to enter.Have some idea?

  • Another thing, I wanted to trigger an event by clicking F12, so I would need to create an F12 button in order to trigger such an Event??

  • Could you put that in another question? Here is just for comments ... there will be visible to more people help you.

Browser other questions tagged

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