-1

I have a SplitPane and a Pane together in a stack. The Pane is underneath the SplitPane in the stack, and the middle SplitPane is a transparent AnchorPane. My intention is for an event to fire which draws a circle on the underlying Pane when I click somewhere within the area of the center pane, but I'm unable to get the event to fire by clicking on the pane.

(EDIT:This mouse event should come from the Pane, not from the anchorpane, because I also need the drawn shapes to respond to clicks.)

I have read posts on here about this and most solutions include MouseTransparent and PickOnBounds. I have tried what feels like every combination of those properties on the central AnchorPane, its label child, and the underlying Pane where the circle needs to be drawn to no avail. Any help is welcome! :)

Here are two images of both the physical layout and the hierarchy in scene builder. The blue showing is the underlying Pane. It is visible because the central AnchorPane is transparent.

Image 1 Image 2

paullc
  • 27
  • 6

1 Answers1

0

Update: You can use setMouseTransparent(true/false) for the foreground split pane when entering/exiting the center anchor pane with the mouse like e. g. this:

Controller Class:

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;

public class Controller {

    @FXML
    private Pane
            backgroundPane;
    @FXML
    private SplitPane
            foregroundSplitPane;

    @FXML
    public void handleBackgroundPaneOnMouseClick() {
        // Create and add an ellipse:
        Ellipse ellipse = new Ellipse();
        ellipse.setRadiusX(50);
        ellipse.setRadiusY(50);
        ellipse.setFill(Color.BLACK);
        ellipse.setLayoutX((backgroundPane.getWidth() / 2));
        ellipse.setLayoutY((backgroundPane.getHeight() / 2));
        backgroundPane.getChildren().add(ellipse);
    }

    @FXML
    public void handleCenterAnchorPaneOnMouseEntered() {
        foregroundSplitPane.setMouseTransparent(true);
    }

    @FXML
    public void handleCenterAnchorPaneOnMouseExited() {
        foregroundSplitPane.setMouseTransparent(false);
    }
}

FXML File:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<StackPane prefHeight="150.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Pane fx:id="backgroundPane" onMousePressed="#handleBackgroundPaneOnMouseClick" style="-fx-background-color: blue;" />
      <SplitPane fx:id="foregroundSplitPane" dividerPositions="0.1, 0.9" style="-fx-background-color: rgba(255,255,255,0);">
        <items>
          <AnchorPane style="-fx-background-color: tomato;" />
          <AnchorPane onMouseEntered="#handleCenterAnchorPaneOnMouseEntered" onMouseExited="#handleCenterAnchorPaneOnMouseExited" style="-fx-background-color: rgba(255,255,255,0);" />
            <AnchorPane style="-fx-background-color: tomato;" />
        </items>
      </SplitPane>
   </children>
</StackPane>

Preview:

start

after click on center pane

anko
  • 1,628
  • 1
  • 6
  • 14
  • Yeah, that works, but I realize my initial question wasn't too clear.What I'm attempting is to have the pane that lies underneath to be the one that is detecting the click. Right now it is being blocked by the center anchorPane. I could just have the anchorpane draw items to the Pane but that would be a bit of a hassle. – paullc May 27 '20 at 22:12
  • Sorry for the missunderstanding. I made an update. Does it work now how u want it to work? – anko May 27 '20 at 23:54
  • Thanks! The hover mouse transparence seems like a fair approach. I'm gonna try that! – paullc May 28 '20 at 01:09