1

我一直在尝试让 GridPane 的内容在自动大小方面表现得很好,但我对 JavaFX 比较陌生,所以我没有取得任何成功。基本上,每一列在我看来都是完全随机的大小,我不知道如何让它们正确增长。如果我尝试设置列约束,内容有时会完全消失,有时只是完全任意调整大小。尝试将首选宽度绑定到其他任何内容也无法正常工作。不过,我确定错误是我的。这是我的代码(contentHolder 是一个 ScrollPane,vGrow 设置为 ALWAYS):

    int row = 1;
    GridPane gridPane = new GridPane();
    for (List<IndexObject> stepList : search) {
        Label itemLabel = new Label();
        itemLabel.setText(String.valueOf(row));

        TreeItem<String> treeRoot = new TreeItem<>(stepList.get(0).getPath());
        for (int line = 1; line < stepList.size(); line++) {
            treeRoot.getChildren().add(new TreeItem<>(stepList.get(line).getPath()));
        }
        TreeView treeView = new TreeView();
        treeView.setShowRoot(true);
        treeView.setRoot(treeRoot);
        treeRoot.setExpanded(true);

        IndexObject lastObject = stepList.get(stepList.size() - 1);

        TextArea output = new TextArea();
        output.setText(lastObject.prettyPrint());

        gridPane.addRow(row, itemLabel, treeView, output);
        row++;
    }
    contentHolder.setContent(gridPane);

直到 contentHolder 的所有内容都在 fxml 中定义。这是它的样子。自动分配的高度附近没有任何条目,但所有宽度都太小。我错过了什么?

格式失败

4

1 回答 1

1

答案是滚动窗格在您制作之前不会自动调整大小:

<ScrollPane VBox.vgrow="ALWAYS" fitToWidth="true">

此时一切都按预期工作。

于 2013-08-13T08:48:09.590 回答