JavaFX - handle MouseEntered event on a button (with fxml)
10,034
You shoud not put another listener onto a control to get it to execute the function. What you are doing is adding another listener every single time you call your handle method.
Use onMouseEntered="#methodToBeCalled"
in FXML, and in your code just create that method:
@FXML
public void methodToBeCalled(){
System.out.println("mouse entered");
}
It's that simple. The method will be called, and all you have to do is specify id/method name, and use annotation.
Author by
Mark
Updated on June 04, 2022Comments
-
Mark about 2 years
I'm trying to learn event handling and made an example with an fxml button that looked like that:
<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 fine. Now I would like to handle the event of entering the button with the mouse. I tried
@FXML private void Handle () { btn_welcome.setOnMouseEntered((event) -> { System.out.println("test"); });
but it doesn't seem to work.
-
Tomas Bisciak about 8 yearsI recommend you to download and use SceneBuilder, you gonna learn javafx faster, and be more productive.