I have two layers (= AnchorPanes) stacked one of the other with a StackPane. So both layer fill the whole scene. The problem is, that only the top layer receives mouse events.
Thats how the scene is build:
Only Button B receives click events but Button A not.
If I set Layer B to mouse transparent (layerB.setMouseTransparent(true)
), Button A receives mouse events. But mouse transparent effects also all children, so Button B dont receives mouse events any more.
How to get Button A and Button B to receive mouse events?
Here is the full working source:
public class LayerTest extends Application {
@Override
public void start(final Stage primaryStage) throws Exception {
final Node layerA = createLayerA();
final Node layerB = createLayerB();
final Parent root = new StackPane(layerA, layerB);
final Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setWidth(250);
primaryStage.setHeight(200);
primaryStage.show();
}
private Node createLayerA() {
final AnchorPane layerA = new AnchorPane();
final Button buttonA = new Button("Button A");
AnchorPane.setLeftAnchor(buttonA, 10d);
AnchorPane.setTopAnchor(buttonA, 10d);
buttonA.setOnMouseClicked(e -> System.out.println("Button A clicked"));
layerA.getChildren().setAll(buttonA);
return layerA;
}
private Node createLayerB() {
final AnchorPane layerB = new AnchorPane();
final Button buttonB = new Button("Button B");
AnchorPane.setRightAnchor(buttonB, 10d);
AnchorPane.setBottomAnchor(buttonB, 10d);
buttonB.setOnMouseClicked(e -> System.out.println("Button B clicked"));
layerB.getChildren().setAll(buttonB);
return layerB;
}
public static void main(String[] args) {
launch(args);
}
}