如何强制ScrollPane填充父vbox

时间:2014-10-02 17:55:52

标签: java user-interface javafx

我在VBox里面的ScrollPane和ScrollPane里面都有ListView,所有这些都在SplitPane中。如何强制ScrollPane适合和填充VBox。它应该通过抵抗来调整场景和分割窗格的大小。我尝试通过在场景调整大小上添加侦听器并设置滚动窗格的pref宽度和高度(取自父VBox)来做到这一点,但它总是不起作用。

我在纯java中做所有事情,我想避免使用fxml。

以下是示例代码

主类中的某个地方(扩展应用程序):

private void setupLayout()
{
    BorderPane lvBorderPane = new BorderPane();

    String lvSplitPaneStyle =
            "-fx-background-color: #FFFFFF;-fx-padding: 10px";

    SplitPane lvMainContainer = new SplitPane();
    lvMainContainer.setOrientation(Orientation.VERTICAL);
    lvMainContainer.setStyle(lvSplitPaneStyle);

    VBox mLeftContainer = new VBox();

    mLeftContainer.setStyle("-fx-background-color: #000000");
    mLeftContainer.setMinWidth(140);
    mLeftContainer.setPrefWidth(220);

    VBox lvCenterContainer = new VBox();

    lvCenterContainer.setStyle("-fx-background-color: #000000;");

    VBox lvRightContainer = new FooBarList(mScene);

    HBox lvBottomContainer = new HBox();

    lvBottomContainer.setStyle("-fx-background-color: #000000;");
    lvBottomContainer.setMinHeight(40);
    lvBottomContainer.setMaxHeight(40);

    SplitPane lvTopPane = new SplitPane();
    lvTopPane.setOrientation(Orientation.HORIZONTAL);

    lvMainContainer.getItems().addAll(lvTopPane, lvBottomContainer);
    lvTopPane.getItems().addAll(mLeftContainer, lvCenterContainer, lvRightContainer);

    lvBorderPane.setCenter(lvMainContainer);
    mRoot.getChildren().add(lvBorderPane);
}

FooBarList类:

public class FooBarList extends VBox 
{
    public FooBarList(Scene pmScene)
    {
        this.mWindow = pmScene;
        setupLayout();
    }

    private void setupLayout()
    {
        this.setStyle("-fx-background-color: #000000;");
        this.setMinWidth(240);
        this.setPrefWidth(240);

        mContainer = new ListView<String>();
        ObservableList<String> lvItems = FXCollections.observableArrayList("");

        this.mContainer.setItems(lvItems);

        this.mScrollPane = new ScrollPane(mContainer);
        this.getChildren().add(mScrollPane);

        this.mScrollPane.setFitToHeight(true);
        this.mScrollPane.setFitToWidth(true);

        final VBox lvParent = this;

        mWindow.widthProperty().addListener(new ChangeListener<Number>() {

            @Override
            public void changed(ObservableValue<? extends Number> observable,
                    Number oldValue, Number newValue) {

                mScrollPane.setPrefWidth(lvParent.getWidth());
            }

        });

        mWindow.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable,
                    Number oldValue, Number newValue) {

                mScrollPane.setPrefHeight(lvParent.getHeight());
            }
        });
    }

    private ScrollPane mScrollPane;
    private ListView<String> mContainer;
    private Scene mWindow;
}

0 个答案:

没有答案