문제

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");
도움이 되었습니까?

해결책

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

Working on a project and facing the same issue

@FXML

TableColumn nameOfTableColumn;

if you pass in nameofTableColumn.getText(), you will return the name of the column (nameOfTableColumn in this case). Add this code accordingly and set a breakpoint on the same line. Step into and hit the +bar over the name of your respective column: one of the exposed fields will give you the value for the column which again, in this case will be "nameOfTableColumn".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top