After digging, I found out how to get the property name of a column. With that I went ahead and wrote some generic reflections to force update. I wrapped everything in a method commit(Object val)
for ease of use. These are modifications to EditCell
class used here.
Disclaimer: This only works if you use a PropertyValueFactory
and follow naming conventions in your row classes. This is also very fickle code, use and modify at your own discretion.
I modified the cell to be a generic cell with public static class EditingCell<S, T> extends TableCell<S, T>
. Everything else from the tutorial should still be the same, if not feel free to let me know and I'll update here accordingly.
public void commit(Object val) {
// Get the table
TableView<S> t = this.getTableView();
// Get the selected row/column
S selectedRow = t.getItems().get(this.getTableRow().getIndex());
TableColumn<S, ?> selectedColumn = t.getColumns().get(t.getColumns().indexOf(this.getTableColumn()));
// Get current property name
String propertyName = ((PropertyValueFactory) selectedColumn.getCellValueFactory()).getProperty();
// Create a method name conforming to java standards ( setProperty )
propertyName = ("" + propertyName.charAt(0)).toUpperCase() + propertyName.substring(1);
// Try to run the update
try {
// Type specific checks - could be done inside each setProperty() method
if(val instanceof Double) {
Method method = selectedRow.getClass().getMethod("set" + propertyName, double.class);
method.invoke(selectedRow, (double) val);
}
if(val instanceof String) {
Method method = selectedRow.getClass().getMethod("set" + propertyName, String.class);
method.invoke(selectedRow, (String) val);
}
if(val instanceof Integer) {
Method method = selectedRow.getClass().getMethod("set" + propertyName, int.class);
method.invoke(selectedRow, (int) val);
}
} catch (Exception e) {
e.printStackTrace();
}
// CommitEdit for good luck
commitEdit((T) val);
}
and then since the text field won't update, I forced an update on it in cancelEdit()
. This is slightly specific to my case ( I want a default of 0.0 and only accepted values are doubles ) - modify it as needed.
@Override
public void cancelEdit() {
super.cancelEdit();
// Default value
String val = "0.0";
// Check to see if there's a value
if (!textField.getText().equals(""))
val = textField.getText();
// Set table cell text
setText("" + val);
setGraphic(null);
}