JavaFX:GridPane和列约束

时间:2016-12-27 22:01:09

标签: javafx javafx-8 fxml scrollpane gridpane

我有一个 ScrollPane控件,里面有左右两部分。左侧部分包含 GridPane控件。我想在父 VBox 控件的整个宽度上设置 GridPane

为此,我使用了 columnConstraints 。现在, GridPane 是全角,但滚动被破坏了。 我该如何解决?

请在使用和不使用columnConstraints的情况下尝试以下示例,您将看到我的意思。

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>

使用相应的 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 个答案:

没有答案