Reputation: 1647
As a Swing programmer, I can't believe this is so hard, but apparently it is.
I've got a javafx TableView, and I want to change one of the values of the model and have the screen update properly.
I do NOT want to use SimpleStringProperty and its ilk for a number of reasons. One is I want to use POJOs. Another is that my data elements are not Strings!! (neither are they simple data types like Integers). So using these is out of the question.
Neither do I want to know about any hacks like hiding and unhiding columns and any nonsense like this. I want to know the Right Way(™) to do it, and I'll leave hacks for others.
The way to do it in Swing is of course trivial. You would call model.fireTableRowsUpdated(index, index);
Upvotes: 0
Views: 286
Reputation: 18425
I feel you. A table is among the most common things in an application and the way it is implemented in JavaFX is most embarrassing (the focus bug where you lose the data when you change the cell is still not solved) and absolutely unusable in a real world environment. No wonder they call it TableVIEW, because you hardly can do anything different with it.
I tried to implement a simple copy/paste feature in which you copy the contents of once cell to another. It's not easy. However, I solved it in a way where I get the property of the cell and invoke the set method.
Excerpt:
if( observableValue instanceof DoubleProperty) {
try {
double value = numberFormatter.parse(clipboardCellContent).doubleValue();
((DoubleProperty) observableValue).set(value);
} catch (ParseException e) {
e.printStackTrace();
}
}
else if( observableValue instanceof IntegerProperty) {
try {
int value = NumberFormat.getInstance().parse(clipboardCellContent).intValue();
((IntegerProperty) observableValue).set(value);
} catch (ParseException e) {
e.printStackTrace();
}
}
else if( observableValue instanceof StringProperty) {
((StringProperty) observableValue).set(clipboardCellContent);
} else {
System.out.println("Unsupported observable value: " + observableValue);
}
You can get the full code from this gist. It may give you an idea how to work around this issue.
Regarding POJOs: You'll have to use Properties. And regarding other types, use the proper ones, DoubleProperty, ObjectProperty, etc.
And it's observable, once you change the data, the tableview will update.
Upvotes: 1