JavaFX: The GridPane and column constraints

3.7k views Asked by At

I have a ScrollPane control with left and right parts inside. Left part contains a GridPane control. I would like to make the GridPane across the entire width of the parent VBox control.

For this purpose, I used a columnConstraints. Now, the GridPane is full-width, but the scrolling is broken. How can I fix it?

Please try the following sample with and without using the columnConstraints and you will see what I mean.


sample.FXML:

<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.Text?>

<ScrollPane fx:controller="sample.Controller"
            xmlns:fx="http://javafx.com/fxml"
            fitToWidth="true"
            fitToHeight="true">
    <HBox>
        <AnchorPane fx:id="leftPane"
                    minWidth="500"
                    prefWidth="500"
                    maxWidth="500"
                    style="-fx-background-color: orange">
            <VBox style="-fx-background-color: lime"
                  AnchorPane.topAnchor="0"
                  AnchorPane.leftAnchor="0"
                  AnchorPane.rightAnchor="0"
                  AnchorPane.bottomAnchor="10">
                <GridPane fx:id="gridPane"
                          style="-fx-background-color: brown">
                    <columnConstraints>
                        <ColumnConstraints />
                        <!-- This causes the trouble! -->
                        <ColumnConstraints hgrow="ALWAYS" />
                    </columnConstraints>
                </GridPane>
                <Label>Left Content</Label>
            </VBox>
        </AnchorPane>
        <AnchorPane fx:id="rightPane"
                    HBox.hgrow="ALWAYS"
                    style="-fx-background-color: purple">
            <Text>Right Content</Text>
        </AnchorPane>
    </HBox>
</ScrollPane>

with the corresponding Controller.java:

    public class Controller implements Initializable {
    @FXML
    private GridPane gridPane;
    private String[] splittedText;

    @Override
    public void initialize(URL location, ResourceBundle resources) {    
        // From Wikipedia
        final String text = "Dolphins are a widely distributed and diverse group of aquatic mammals. They are an informal grouping within the order Cetacea, excluding whales and porpoises, so to zoologists the grouping is paraphyletic. The dolphins comprise the extant families Delphinidae (the oceanic dolphins), Platanistidae (the Indian river dolphins), Iniidae (the new world river dolphins), and Pontoporiidae (the brackish dolphins). There are 40 extant species of dolphins. Dolphins, alongside other cetaceans, belong to the clade Cetartiodactyla with even-toed ungulates.";
        splittedText = text.split(" ");

        for (int i = 0; i < 20; ++i) {
            gridPane.add(new Text(Integer.toString(i)), 0, i);
            gridPane.add(createFlowPane(), 1, i);
        }
    }

    private FlowPane createFlowPane() {
        FlowPane flowPane = new FlowPane();
        for (int i = 0; i < splittedText.length; ++i)
            flowPane.getChildren().add(new Text(splittedText[i]));

        return flowPane;
    }
}
0

There are 0 answers