Issue
I'll try to explain this as best I can. I've only started looking into JavaFX for the first time this past week. I'm still grasping how to use PropertyValueFactory and SimpleStringProperty, but I make it work. Now, if I have 20 columns that's 20 get/set methods I'd need to write. Not a huge issue, but tedious and seems long-winded to me.
Is there a way I can have all the columns call the same method for data retrieval but pass in a variable? Like either a String or an enum? So the object in my TableView's ObservableList would have a method like:
class TableObject{
public String getTableElement(String key){
return this.data.get(key);
}
}
Hopefully that makes sense, I wasn't quite sure how to word this question.
Solution
Implement your own Callback
and replace the PropertyValueFactory
.
private static TableColumn<TableObject, String> createColumn(String name, final String key) {
TableColumn<TableObject, String> column = new TableColumn<>(name);
column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getTableElement(key)));
return column;
}
Answered By - fabian