3

I am working on a project, where I have a GUI(BorderPane) and a movableMap(Pane) in a StackPane, but when I want to click on the map or one of his child the event handler handels only on the GUI. How can I fix this problem?

I put all my objects in a Map pane, and i move this Pane in an another Pane.

And here is some code:

Pane Map = new Pane();
Map.getChildren().addAll(...);
Map.setLayoutX(0);
Map.setLayoutY(0);
Map.setOnMouseClicked(e-> System.out.println("Click1"));

GUI = new BorderPane();
GUI.getChildren().addAll(...);
GUI.setOnMouseClicked(e -> System.out.println("Click2"));

MovableMap = new Pane();
MovableMap.getChildren().add(Map);
MovableMap.setOnMouseClicked(e -> System.out.println("Click3"));

StackPane root = new StackPane();
root.getChildren().addAll(MovableMap,GUI);
root.setOnMouseClicked(e -> System.out.println("Click4"));
Neuron
  • 5,141
  • 5
  • 38
  • 59
Balinator
  • 106
  • 11
  • Possible duplicate of [When adding a second item to my stackpane, the first item loses its Event/MouseOn. Why? How can I fix? JavaFX](http://stackoverflow.com/questions/9899347/when-adding-a-second-item-to-my-stackpane-the-first-item-loses-its-event-mouseo) – randers Dec 20 '15 at 10:24
  • By the way, Java naming conventions are that you should start variable names lowercase. That makes them distinguishable from static methods of classes (e.g. if you name your variable `Map` and then call methods on it, it's not obvious for the reader and the future yourself whether the method is called on an object of some type or statically on the class `java.util.Map`.) – randers Dec 20 '15 at 10:26

2 Answers2

0

Some time ago I had exactly the same problem. All panes (not only the StackPane) seem to swallow all events when you somehow put them on top of each other. I was only able to solve this problem by doing the layout of the GUI pane(s) manually. Conceptually I did something like this:

    private BorderPane root;
    private MapPane mapPane;
    private GUIPane guiPane;

    @Override
    public void start(Stage stage) {
        root = new BorderPane();
        mapPane = new MapPane();            
        guiPane = new GUIPane();

        guiPane.setManaged(false);

        root.setCenter(mapPane);
        root.getChildren().add(guiPane);

        Scene scene = new Scene(root, 800, 600);

        stage.setScene(scene);
        stage.show();

        guiPane.autosize();
        guiPane.layoutXProperty().bind(root.widthProperty().subtract(guiPane.widthProperty()).divide(2));
        guiPane.setLayoutY(20);
    }

It works for me but I'd love to hear of a better solution of doing this.

mipa
  • 10,369
  • 2
  • 16
  • 35
0

You can make any Pane "mouse transparent", so that it doesn't consume any click events, and lets them pass through to the stack under it.

Here's some example code... this example sets up 4 panes in a stack, with just the mainPane accepting clicks to begin with.

StackPane rootPane = new StackPane();
VBox mainPane = new VBox(80);

BorderPane helpOverlayPane = new BorderPane();
helpOverlayPane.setMouseTransparent(true);

Canvas fullScreenOverlayCanvas = new Canvas();
fullScreenOverlayCanvas.setMouseTransparent(true);

VBox debugPane = new VBox();
debugPane.setAlignment(Pos.BASELINE_RIGHT);
AnchorPane debugOverlay = new AnchorPane();
debugOverlay.setMouseTransparent(true);
debugOverlay.getChildren().add(debugPane);
AnchorPane.setBottomAnchor(debugPane, 80.0);
AnchorPane.setRightAnchor(debugPane, 20.0);

rootPane.getChildren().addAll(mainPane, fullScreenOverlayCanvas, debugOverlay, helpOverlayPane);

Now, when you want to use your canvas to draw on top, make sure you change mouse transparent to false for just that stack, and keep all panes on top of it mouse transparent.

fullScreenOverlayCanvas.setMouseTransparent(false);
debugOverlay.setMouseTransparent(true);
fullScreenOverlayCanvas.setVisible(true);

doSomethingWithCanvasThatNeedsMouseClicks();

P.S. I did some editing of the code I had, so it may not run as-is. Also, see discussion of making only parts of panes transparent here: JavaFX Pass MouseEvents through Transparent Node to Children

https://stackoverflow.com/a/21062186/4464702

Community
  • 1
  • 1
randers
  • 5,031
  • 5
  • 37
  • 64
  • I don't know the original authors intent but switching the mouse transparency of the panes is no option for me. It is more like the problem you refer to in your last link (JavaFX Pass MouseEvents ...) but I never got that example working and also the code looks very dubious to me. I just don't see what positive effect this pickOnBounds attribute should have in this context. – mipa Dec 20 '15 at 18:41