JavaFX - Get TableView column by name


Question

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");
1
3
4/21/2014 12:30:28 AM

Accepted Answer

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 ;
}
9
4/21/2014 12:42:45 AM

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());

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon