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.
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.
you can also try this
btn_welcome.addEventHandler(MouseEvent.MOUSE_ENTERED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
//your code here
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.