JavaFX - handle MouseEntered event on a button (using fxml)

I am trying to learn about event handling and made an example with the fxml button, which looked like this:

<Button fx:id="button" onAction="#Handle">

and the following handler method in my controller:

@FXML
 private void Handle () {

    btn_welcome.setOnMouseClicked((event) -> {

        System.out.println("test");

    });

So far, this works great. Now I would like to handle the button input event with the mouse. I tried

@FXML
 private void Handle () {

    btn_welcome.setOnMouseEntered((event) -> {

        System.out.println("test");

    });

but it does not work.

+4
source share
2 answers

You do not put another listener on the control to force it to execute the function. What you do, you put another listener every time you call your descriptor method.

use onMouseEntered="#methodToBeCalled"in fxml

and in code just create this method

@FXML
public void methodToBeCalled(){
   System.out.println("mouse entered");
}

.Method , , , id/method .

+5

 btn_welcome.addEventHandler(MouseEvent.MOUSE_ENTERED,
        new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent e) {
           //your code here
          }
        });
-1

All Articles