Egor Hans
Egor Hans

Reputation: 206

Line drawn over GridPane won't show

I need a line separator in a GridPane. Not all lines visible, but rather one single in the middle. The way I'm currently doing it is putting in a Pane with a Line that overlays the GridPane:

StackPane secondSlide = new StackPane();
GridPane secondSlideCore = new GridPane();
// secondSlideCore is being filled with TextFields
secondSlide.getChildren().add(secondSlideCore);
Pane secondSlideOverlay = new Pane();

secondSlideOverlay.setPrefSize(secondSlideCore.getPrefWidth(), secondSlideCore.getPrefHeight());
Line mainOverlay = new Line(secondSlideOverlay.getPrefWidth() / 2, 0, secondSlideOverlay.getPrefWidth() / 2, secondSlideOverlay.getPrefHeight());

Later on I piece everything together:

secondSlideOverlay.getChildren().add(mainOverlay);
secondSlide.getChildren().add(secondSlideOverlay);

Even though secondSlide is being correctly added to the window (secondSlideCore does show), the line won't appear for some reason.

I tried all of the following to make it show:

mainOverlay.setFill(Color.BLACK);
mainOverlay.setStroke(Color.BLACK);
mainOverlay.setStrokeWidth(2);
mainOverlay.setVisible(true);
mainOverlay.setOpacity(1);
secondSlideOverlay.setVisible(true);

Still no line over my GridPane. Am I missing something?

Upvotes: 1

Views: 148

Answers (1)

fabian
fabian

Reputation: 82461

prefWidth and prefHeight default to the constant Region.USE_COMPUTED_SIZE which has the value of -1.0. For this reason you get more of a dot than a line ((-0.5, 0) to (-0.5, -1)).

Furthermore resizing the pane won't keep the line up to date.

Use bindings to fix this:

Pane secondSlideOverlay = new Pane();

secondSlideOverlay.prefWidthProperty().bind(secondSlideCore.widthProperty());
secondSlideOverlay.prefHeightProperty().bind(secondSlideCore.heightProperty());

DoubleExpression w2 = secondSlideCore.widthProperty().multiply(.5);

Line mainOverlay = new Line();
mainOverlay.startXProperty().bind(w2);
mainOverlay.endXProperty().bind(w2);
mainOverlay.endYProperty().bind(secondSlideCore.heightProperty());

Upvotes: 1

Related Questions