Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - Get TableView column by name

Is there a way to get a column from a TableView by name?

When I need to get a column I have to get it by index:

tableView.getColumns().get(i);

but I would like to get the column by name:

tableView.getColumns().get("Column Name");
like image 322
ceklock Avatar asked Apr 21 '14 00:04

ceklock


People also ask

How to sort TableView in JavaFX?

The JavaFX TableView enables you to sort the rows in the TableView. There are two ways rows can be sorted. The first way is for the user to click on the TableColumn header cell (where the column title is displayed). This will sort the rows in the TableView after the values of that column.

What is setcellfactory?

The cell factory for all cells in this column. The cell factory is responsible for rendering the data contained within each TableCell for a single table column.

What does setCellValueFactory do?

setCellValueFactory. Sets the value of the property cellValueFactory.

What does TableView refresh do?

Calling refresh() forces the TableView control to recreate and repopulate the cells necessary to populate the visual bounds of the control. In other words, this forces the TableView to update what it is showing to the user.


2 Answers

It's hard to envision a situation in which you couldn't just keep references to your columns, but you can always write a method like

private <T> TableColumn<T, ?> getTableColumnByName(TableView<T> tableView, String name) {
    for (TableColumn<T, ?> col : tableView.getColumns())
        if (col.getText().equals(name)) return col ;
    return null ;
}
like image 114
James_D Avatar answered Sep 21 '22 22:09

James_D


Another way of getting the column name or title is setting an ID to the column and retrieve it when necessary:

  private String col_ID = "Customer";
  private TableColumn col = new TableColumn (col_ID);
  col.setId(col_ID);

  System.out.println(col.getId());
like image 24
rainer Avatar answered Sep 17 '22 22:09

rainer