2
有沒有辦法從名稱中獲取TableView列?JavaFX - 按名稱獲取TableView列
當我需要得到一個專欄中,我必須通過索引來得到它:
tableView.getColumns().get(i);
,但我想通過名稱來獲得柱:
tableView.getColumns().get("Column Name");
有沒有辦法從名稱中獲取TableView列?JavaFX - 按名稱獲取TableView列
當我需要得到一個專欄中,我必須通過索引來得到它:
tableView.getColumns().get(i);
,但我想通過名稱來獲得柱:
tableView.getColumns().get("Column Name");
這是很難想象的情況其中你不能只保留對你的專欄的引用,但你總是可以寫一個方法,如
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 ;
}
另一種方法獲得列名或標題設置一個ID列和檢索它在必要時:
private String col_ID = "Customer";
private TableColumn col = new TableColumn (col_ID);
col.setId(col_ID);
System.out.println(col.getId());
是的,我可以保持一個HashMap中的列引用,但我不知道是否有東西在API中獲得他們的名字。 – ceklock
不,沒有在API中。我懷疑這不是一個常見的用例;在絕大多數情況下,這些列是完全靜態的,所以您將擁有實際的命名引用,甚至不需要「Map」。 –