0

how can i create column and set their name from a list of String in JavaFx. Is there a methode like public String getColumnName(int column) and getColumnCountas in swingx in JavaFx?

Iman
  • 769
  • 1
  • 13
  • 51

1 Answers1

2

No, the data model for a JavaFX TreeTableView is managed differently to the way the data model is managed in Swing.

The data model is represented by two properties in the TreeTableView class: root and columns.

root is a TreeItem<T> (where T is the data type represented by each row of the TreeTableView). It has a getChildren() method for child elements in the tree structure, expanded state, etc.

getColumns() returns an ObservableList<TreeTableColumn<T,?>>, which is essentially a list of columns. So the number of columns is determined by the size of this list. TreeTableColumn has a text property which encapsulates the title of the column. So the column names are represented by the text properties of the elements of the list returned by getColumns().

So, to sort of answer your question, given

TreeTableView<T> treeTable ;

(where T is replaced by some actual type), and

List<String> columnNames ;

you can populate the columns of the tree table with

for (String name : columnNames) {
    TreeTableColumn<T,?> column = new TreeTableColumn<>(name);
    treeTable.getColumns().add(column);
}

or if you prefer a Java 8 style:

columnNames.stream().map(TreeTableColumn::new)
    .forEach(treeTable.getColumns()::add);

However, note that you almost certainly need to set a cell value factory on each column, and possibly configure it further in other ways.

James_D
  • 201,275
  • 16
  • 291
  • 322