I have a pane in Javafx that a want to animate whenever the mouse has entered its boundaries and I want the animation to stop once the mouse has exited the pane. I understand this calls for a listener but all the answers I find only seem to concern java.awt
You can use the methods Node.setOnMouseEntered()
and Node.setOnMouseExited()
to register event handlers, which start or stop your animation. Here is a simple example:
public class MainTest extends Application {
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setStyle("-fx-background-color: #ff0000");
pane.setLayoutX(100);
pane.setLayoutY(100);
pane.setPrefSize(300,300);
pane.setOnMouseEntered(event -> startAnimation());
pane.setOnMouseExited(event -> stopAnimation());
Scene scene = new Scene(new Pane(pane), 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
private void stopAnimation() {
System.out.println("stop");
// do whatever you need to start your animation
}
private void startAnimation() {
System.out.println("start");
// do whatever you need to stop your animation
}
}
Or you can make it with chain of *.fxml file and your controller class:
onMouseEntered="#onMouseInto" onMouseExited="#onMouseOut"
to make it like<AnchorPane fx:id="rootPane" onMouseEntered="#onMouseInto" onMouseExited="#onMouseOut" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="yourPackage.YourControllerClass">
@FXML
public AnchorPane rootPane;
public void onMouseInto(MouseEvent mouseEvent) {
//Your own event when cursor is gonna into the rootPane
rootPane.setStyle("-fx-background-color: #1F292E");
}
public void onMouseOut(MouseEvent mouseEvent) {
//Your own event when cursor is gonna out the rootPane
rootPane.setStyle("-fx-background-color: #C792EA");
}
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.