The JavaFX MouseEvent
is only delivered to the top-most node which is not mouse transparent. I'm looking for a way to deliver a MouseEvent
to multiple nodes (mouse translucent, maybe?).
In the example below, I have 2 partially overlapping circles. The top circle listens to MOUSE_CLICKED
events to change its color. The bottom circle receives MOUSE_ENTERED
and MOUSE_EXITED
to update its hover property, changing its color while the mouse is over it.
When the top circle is mouse transparent, the bottom circle behaves as desired, but the top circle no longer receives MOUSE_CLICKED
events. If the top circle is not mouse transparent, then the bottom circle sees MOUSE_EXITED
when the mouse passes over the top circle, even if the mouse remains inside the bottom circle's shape.
It is possible to support both behaviors simultaneously?
public class MainApp extends Application {
private final Random RND = new Random();
@Override
public void start(Stage stage) throws Exception {
Circle bottomCircle = new Circle(150, 150, 100, Color.BLUE);
bottomCircle.fillProperty().bind(Bindings.when(bottomCircle.hoverProperty()).then(Color.AQUA).otherwise((Color.BLUE)));
Circle topCircle = new Circle(200, 100, 40, randColor());
topCircle.setOnMouseClicked((event) -> topCircle.setFill(randColor()));
CheckBox mouseTransparencyCheckBox = new CheckBox("Top Circle Mouse Transparency");
topCircle.mouseTransparentProperty().bind(mouseTransparencyCheckBox.selectedProperty());
Pane pane = new Pane();
pane.setPrefSize(300, 300);
pane.getChildren().addAll(mouseTransparencyCheckBox, bottomCircle, topCircle);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
private Color randColor() {
return Color.hsb(RND.nextDouble() * 360, 1, 1, 0.75);
}
public static void main(String[] args) {
launch(args);
}
}