如何使TreeView调整大小与父ScrollPane一样多?

时间:2013-09-30 13:09:38

标签: java treeview javafx-2

我将TreeView作为ScrollPane的内容放置。 ScrollPane放在SplitPane中。 当我拖动SplitPane的分隔符使它变得比TreeView大时更大时,我看到TreeView的边框。 在拖动SplitPane的分隔符后,我希望TreeView可以调整空间大小。我的代码:

SplitPane splitPane = new SplitPane();
splitPane.getItems().addAll(createTreeOfConnections());

private ScrollPane createTreeOfConnections() {
ScrollPane scrollPane = new ScrollPane();
scrollPane.setMinSize(100, 300);
scrollPane.setPrefSize(200, 500);
    scrollPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setContent(new ConnectionsTree(this));
return scrollPane;

类ConnectionsTree扩展了TreeView,它的构造函数是:

public ConnectionsTree(MainStage mainStage) {
        // here we set the root of ConnectionsTree which is not visible
        super();
        this.mainStage = mainStage;

        root = new ConnectionTreeItem();
        root.setExpanded(true);
        super.setRoot(root);
        super.setShowRoot(false);
        super.setEditable(false);
        super.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        super.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

        retrieveExistentConnectionsNodes();
        ContextMenu contextMenu = new ContextMenu(createNewConnectionMenuItem(
                "New Connection", KeyCombination.valueOf("Ctrl+N")));
        super.setContextMenu(contextMenu);                }

如何告诉TreeView(我的TreeConnections)它应该占用SplitPane左侧部分的所有可用空间?

谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个

scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);