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");
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.
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.
setCellValueFactory. Sets the value of the property cellValueFactory.
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.
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 ;
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With