Center an unresizable pane in JavaFX-2


I'm trying to make a simple image viewing application in JavaFX-2, FXML and Scene Builder. It is my first JavaFX application and I'm having trouble playing with panes and layouts.

Basically, I want it to display an image in its original size (without being able to zoom/resize), in the absolute center of my application. Also, the window should be resizable, so if the image is bigger than the window's size, I want to have scrollbars appear to be able to see the entire image.

A good example of what I'm trying to create would be the Adobe Photoshop layout:

This is my current layout: The image is represented by the black Stack Pane. The scrollbars are currently working, but I just can't find a way to put the Stack Pane in the center.

I feel like I am missing something important, can you please point me in the right direction?


edit: here is my fxml code:

7/18/2013 1:44:03 AM

Accepted Answer

you used an AnchorPane as container for your ScrollPane. If you use a StackPane instead, it makes the content center on default. However, a StackPane, like any other Pane, uses as much space as it can get, so it will always take all space up and thus not being centered at all. To solve this, put your ScrollPane inside a Group. A Group is always as big as its children are, and you can control its width and height by setting the prefHeight and prefWidth of the Scrollpane. Heres your FXML with it (I removed the buttons onClick listeners for simplicity of my example):

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<?import javafx.scene.Group?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="700.0" xmlns:fx="" fx:controller="test.StackOverflowController">
    <VBox prefHeight="687.0" prefWidth="710.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <ToolBar prefWidth="600.0" style="-fx-base: firebrick">
            <Button fx:id="btn_ImgFromFile" mnemonicParsing="false" style="-fx-base: firebrick" text="Load Image From File" />
            <Button fx:id="btn_ImgFromWindow" mnemonicParsing="false" style="-fx-base: firebrick" text="Load Image From Window" />
            <Separator orientation="VERTICAL" prefHeight="21.0" />
            <Button fx:id="btn_LoadInfo" mnemonicParsing="false" style="-fx-base: firebrick" text="Load Window Info" />
            <Button fx:id="btn_SaveInfo" mnemonicParsing="false" style="-fx-base: firebrick" text="Save Window Info" />
        <StackPane fx:id="pane_main" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
            <ScrollPane fx:id="scroll_pane" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="-1.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="1.0">
                <StackPane fx:id="stack_pane" prefHeight="230.0" prefWidth="333.0" style="-fx-background-color: #000000;" />


enter image description here

7/18/2013 1:02:46 PM

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