How to make a single column in TreeTableView fill all available space but still show a horizontal scrollbar if content is wider?

6 days ago 8
ARTICLE AD BOX

I have a TreeTableView with a single column. I want the column to fill all available space, but if the content is wider than the table, a horizontal scrollbar should appear.

This is my code

public class TreeTableExample extends Application { @Override public void start(Stage stage) { TreeItem<String> root = new TreeItem<>("Root"); TreeItem<String> parent = root; for (int i = 1; i <= 100; i++) { TreeItem<String> child = new TreeItem<>("Node " + i); parent.getChildren().add(child); parent = child; } TreeTableView<String> table = new TreeTableView<>(root); table.setShowRoot(true); TreeTableColumn<String, String> column = new TreeTableColumn<>("Column"); column.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getValue())); table.getColumns().add(column); table.setColumnResizePolicy(TreeTableView.UNCONSTRAINED_RESIZE_POLICY); VBox vbox = new VBox(table); Scene scene = new Scene(vbox, 400, 400); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(); } }

And this is the result:

enter image description here

As you see, it doesn't work. Could anyone say how to do it?

Read Entire Article