I have a pane, inside the pane I have a circle and a label. Everytime mouse enter or exit the circle, the label will change to "enter" or "exit". And the label will move accordingly to the mouse position (mouse scenceX and sceneY).
The problem is when I enter the mouse inside the circle, the label will change to "enter" but will immediately change to "exit" then.
I think there is an overlap eventhandler somewhere in my code, but I don't know why. This is my code:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Pane pane = new Pane();
pane.setStyle("-fx-background-color: grey;");
pane.setPrefSize(300,300);
Circle circle = new Circle();
circle.setFill(Color.TRANSPARENT);
int radius = 50;
circle.setRadius(radius);
circle.setStroke(Color.BLACK);
circle.setStrokeWidth(2);
circle.setLayoutX(100);
circle.setLayoutY(60);
pane.getChildren().add(circle);
Label label = new Label("Mouse point is outside the circle");
pane.getChildren().add(label);
circle.addEventHandler(MouseEvent.MOUSE_EXITED, e -> {
label.setText("Mouse point is outside the circle");
System.out.println("exit");
});
circle.addEventHandler(MouseEvent.MOUSE_ENTERED, e -> {
label.setText("Mouse point is inside the circle");
System.out.println("enter");
});
pane.addEventFilter(MouseEvent.MOUSE_MOVED, e -> {
label.setLayoutX(e.getSceneX());
label.setLayoutY(e.getSceneY());
});
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(pane));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}