0
votes

I'm ready to pull my hair out over this. I'm working on game GUI interface which has a map consisting of a grid of cells.

The grid of cells is composed of a StackPane and then several layers inside consisting of either ImageViews or Shapes etc.

The whole thing is contained in a GridPane, which comprises the center element of a BorderPane;

I can't add a mouse event to the underlying scene because then all the nodes get it -> no good. I need to highlight the nodes under the mouse.

Here's the code to create the stack:

private void initializePanes() {
    myParent = new StackPane();
    myObjectLayer = new StackPane();
    myOverlayLayer = new StackPane();
    myParent.getChildren().addAll(myObjectLayer, myOverlayLayer);
    myParent.getStylesheets().add(myConfig.getString("StyleSheet"));
}

and Here's the code to set an EventHandler -> nothing works or registers. I've tried EventFilters too - no dice, not unless I write to the underlying scene, which is a No-Go.

private void initializeHandlers() {
    myParent.addEventHandler(MouseEvent.ANY, (e) -> System.out.println("I'm responding"));  
}

Help will be appreciated!

EDIT: here's the issue, The overall application is laid out as so:

BorderPane: Center -> StackPane Within that stackpane I have a ScrollPane, which contains a GridPane, which contains the cells (which I posted code for above).

I'm somewhat new to JavaFX so maybe this isn't a good way to do this. But for some reason, mouse events do not make it past the underlying stackpane -> i.e. they get absorbed by the center panel of the borderpane. I cannot add event handlers to the scroll pane, the grid pane, or any of the cells within the gridpane.

1
There is nothing at all wrong with the code snippets you have posted: the error is likely in the way you have set things up, but there is no possible way for anyone to help without being able to see that. You need to create a minimal reproducible example and edit your question to include it. - James_D
If you won't post a MCVE, it's going to be really hard to answer the question. You could try either making myObjectLayer and myOverlayLayer mouse transparent (setMouseTransparent(true)), or you could try registering the mouse handler with myOverlayLayer instead of myParent. But again, I have no idea if these will work. - James_D
Sorry - working on one - don't want to post a bunch of classes. Weird thing is that if I just layer things on top of one another in a testing class it works fine :( So I'm trying to reproduce it as simply as possible. - Ted Yavuzkurt

1 Answers

0
votes

Figured it out. There was another layer in the main stack pane for the Map -> it was intercepting the calls. Removed it - I'll figure out how to implement it later.