I have a ScrollPane that contains a GridPane that contains an ImageView that contains an image. I want the image to be centered in GridPane.
Without ScrollPane, I was able to accomplish this with setAlignment (Pos.TOP_CENTER), however, as soon as I added a GridPane to the ScrollPane, the image was displayed in the upper left corner.
How do I get ScrollPane to use the value of setAlignment () or manually center the image?
--- editing example ---
code without scrollpane:
public class ImageDB extends Application { @Override public void start(Stage root) { Image image = new Image("0.jpg"); ImageView view = new ImageView(); view.setImage(image); GridPane grid = new GridPane(); grid.getChildren().add(view); grid.setAlignment(Pos.TOP_CENTER); BorderPane border = new BorderPane(); border.setCenter(grid); root.setScene(new Scene(border)); root.setMaximized(true); root.show(); } }
Result: https://i.imgur.com/eWvBQy6.png
code with scrollpane:
public class ImageDB extends Application { @Override public void start(Stage root) { Image image = new Image("0.jpg"); ImageView view = new ImageView(); view.setImage(image); ScrollPane scroll = new ScrollPane(); scroll.setContent(view); GridPane grid = new GridPane(); grid.getChildren().add(scroll); grid.setAlignment(Pos.TOP_CENTER); BorderPane border = new BorderPane(); border.setCenter(scroll); root.setScene(new Scene(border)); root.setMaximized(true); root.show(); } }
Result: https://i.imgur.com/1aJDX4m.png
source share