Dynamic AnchorPane Alignment


Question

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?

1
0
2/20/2013 9:36:26 PM

Accepted Answer

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);
1
3/14/2013 5:52:21 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon