3

ListView appears to already have a scrollbar. I want the scrollbar to always be visible. The reason is because I'm putting a header on it and a button in the corner between the scrollbar and header. How can I get the ListView scrollbar to always display?

mentics
  • 6,852
  • 5
  • 39
  • 93

1 Answers1

5

you could put it into a properly sized ScrollPane and set the vbar policy of the ScrollPane to 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();
  }
zhujik
  • 6,514
  • 2
  • 36
  • 43
  • 1
    Thanks, but your code example does not quite work. The horizontal scroll also shows up unnecessarily, and with a wide value in a column, it gets truncated instead of the horizontal scrollbar working properly. – mentics Jul 02 '13 at 16:44
  • 2
    My code works: You didn't ask for a behavior of the horizontal scrollbar but only for the vertical. If you don't want the horizontal scrollbar to show up you should have asked for that. A simple `pane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);` however is enough to achieve that. – zhujik Jul 03 '13 at 07:49
  • 4
    It's not that simple (I wish it was). The problem is that the ListView is being automatically created at a set default size, which does not match the size of its contents (I posted another question about that). Because it doesn't auto size the ListView, I don't think this solution will work. If you have an extra wide column, you get the ListView's horizontal scrollbar inside the scrollpane. If you add many rows to your code, you'll see they get truncated with no way to see them (or two vertical scrollbars if you don't disable the scrollpane's horizontal scrollbar). – mentics Jul 03 '13 at 10:26