Always show vertical scrollbar for JavaFX ListView

16,600

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();
  }
Share:
16,600

Related videos on Youtube

mentics
Author by

mentics

I enjoy working on complex algorithms, AI, math, physics, and other such interesting challenges.

Updated on October 06, 2022

Comments

  • mentics
    mentics over 1 year

    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
    mentics almost 11 years
    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.
  • zhujik
    zhujik almost 11 years
    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.
  • mentics
    mentics almost 11 years
    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).