Search code examples
javafxbounding-box

JavaFX - Separate Child Bounding Box from Parent


I am having an issue with a graph I have made in JavaFX. I am creating a label and adding it to a point on mouse enter:

Label label = new Label(s);
label.setTranslateY(-20.0);

...

setOnMouseEntered(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent mouseEvent) {
        getChildren().setAll(label);
        toFront();
    }
});

setOnMouseExited(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent mouseEvent) {
        getChildren().clear();
    }
});

This ends up with the point and the label sharing a bounding box.

I would like the bounding boxes of the two to be separate. This way, when the cursor exits the point, the label should disappear. Currently, the label will still display as long as it is in the box generated by the label and point combined.

I would appreciate any help on figuring out how to separate the bounding boxes of these two elements. Thanks!


Solution

  • To solve this, I ended up creating a new empty node which sits at the top middle of the graph. When a point is hovered, the label is added to the newly created node. This allows the user to hover the points without the label getting in the way.