peronium
peronium

Reputation: 3

Dynamic AnchorPane Alignment

I am trying to programatically insert a single image onto my application. The image does show up in its AnchorPane, but the problem is that the AnchorPane (and therefore the ImageView as well) appears to be stuck to the top left corner of the window.

This AnchorPane is a child of a ScrollPane. I want my image/AnchorPane to be aligned to the top center of the ScrollPane, not the top left. Is dynamic centering of an AnchorPane possible?

Upvotes: 0

Views: 7939

Answers (1)

jewelsea
jewelsea

Reputation: 159291

To dynamically center an AnchorPane, place it in a StackPane.


To place the AnchorPane in the top center of the StackPane, call

StackPane.setAlignment(node, Pos.TOP_CENTER)

AnchorPane anchorPane = new AnchorPane();
...
StackPane stackPane = new StackPane();
stack.getChildren().add(anchorPane);
StackPane.setAlignment(anchorPane, Pos.TOP_CENTER);

Upvotes: 1

Related Questions