始终显示JavaFX ListView的垂直滚动条

时间:2013-07-01 20:18:49

标签: javafx-2

ListView似乎已经有了一个滚动条。我希望滚动条始终可见。原因是因为我在它上面放了一个标题,在滚动条和标题之间的角落里放了一个按钮。如何让ListView滚动条始终显示?

1 个答案:

答案 0 :(得分:3)

您可以将其放入适当大小的ScrollPane并将ScrollPane的vbar策略设置为ALWAYS:

  @Override
  public void start(Stage primaryStage) throws Exception {
    ScrollPane pane = new ScrollPane();
    ListView<String> list = new ListView<String>();
    ObservableList<String> items = FXCollections.observableArrayList(
            "Single", "Double", "Suite", "Family App");
    list.setItems(items);
    pane.prefWidthProperty().bind(list.widthProperty());
    pane.prefHeightProperty().bind(list.heightProperty());
    pane.setContent(list);
    pane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
    Group group = new Group();
    group.getChildren().add(pane);
    Scene scene = new Scene(group, 500, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
  }