Situation :
I have two overlapped component which aren't related (different nodes from different parents). The front component absorbs all the events which is my problem. The component's hierarechy cannot be changed, and I am looking to solve this programmatically.
What I want :
I want the front component to be transparent to the MouseClick events (similar to the MouseTransparent(true) behavior but only for MouseClick) so the background component could capture the right click event.
What I tried :
- Deleting EventHandlers of the front component - No effect
- Firing the event after calculation of the background component - Very Instable
Is there a way to make the front component half transparent to specific events?
[EDIT - Code & Illustration]
Note : I wanna find a way to ignore the Left MouseClick on the front.
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
initScene(primaryStage);
}
public void initScene(Stage primaryStage) {
/* Initialization */
StackPane root = new StackPane();
Group frontContainer = new Group();
Group backContainer = new Group();
Scene _scene = new Scene(root, 500, 500);
Rectangle frontComponent = new Rectangle(180, 180);
Rectangle backComponent = new Rectangle(200, 200);
frontComponent.setFill(Color.GREY);
backComponent.setFill(Color.ORANGE);
/* Scene Graph construction */
frontContainer.getChildren().add(frontComponent);
backContainer.getChildren().add(backComponent);
root.getChildren().add(backContainer);
root.getChildren().add(frontContainer);
backContainer.toBack();
frontContainer.toFront();
// --- MouseEvents Handlers
// --------------------------------------------------------
frontContainer.setOnMouseClicked((mouseClicked) -> {
if (mouseClicked.getButton() == MouseButton.PRIMARY)
System.out.println("FrontComponent right click");
});
frontContainer.setOnDragDetected((mouseDrag) -> {
// Does something very important for a DragNDrop feature that
// depends on Right Mouse Click
});
backComponent.setOnMouseClicked((mouseClicked) -> {
if (mouseClicked.getButton() == MouseButton.SECONDARY)
System.out.println("BackComponent left click");
});
// ------------------------------------------------------------------------------------
// frontContainer.setOnMouseClicked(null); /* Disables the handling but
// still consumes the event */
primaryStage.setScene(_scene);
primaryStage.show();
}
}