I want to update a row to be strikethrough in a tableview when the user deletes it. I'm somewhat new to javafx and have been searching with no luck.
donationsTable.setRowFactory(tv -> {
TableRow<Donation> row = new TableRow<Donation>() {
// to force updateItem called
@Override
protected boolean isItemChanged(Donation d,
Donation d2) {
return true;
}
@Override
public void updateItem(Donation d, boolean empty) {
super.updateItem(d, empty) ;
if (d == null) {
setStyle("");
} else if (d.getAction().equals(Donation.DELETE_DONATION)) {
setStyle("delete-row");
} else if (d.getAction().equals(Donation.NEW_DONATION)) {
setStyle("-fx-font-weight: bold;");
} else {
setStyle("");
}
}
};
row.setOnMouseClicked(event -> {
deleteDonation.setDisable(false);
});
return row;
});
The bold works for new donations, but I can't get the strikethrough to work. I did see that it needs to be set on the text, not the row so my css is:
.delete-row .text {
-fx-strikethrough: true;
}
However, I'm getting a warning: WARNING CSS Error parsing '*{delete-row}: Expected COLON at [1,12] I only have a very basic understanding of css. This is what I have seen in other answers, but I don't understand why it is not working for me.
Any help is much appreciated.
Based on James_D's suggestion, I changed updateItem:
public void updateItem(Donation d, boolean empty) {
super.updateItem(d, empty) ;
PseudoClass delete = PseudoClass.getPseudoClass("delete-row");
pseudoClassStateChanged(delete, d != null && d.getAction().equals(Donation.DELETE_DONATION));
PseudoClass add = PseudoClass.getPseudoClass("add-row");
pseudoClassStateChanged(add, d != null && d.getAction().equals(Donation.NEW_DONATION));
}
css has
.table-row-cell:delete-row .text {
-fx-strikethrough: true;
}
.table-row-cell:add-row {
-fx-font-weight: bold;
}
strikethrough still not working and bold stopped working.
The
setStyle
method will set an inline style on aNode
; this style is in the form of a CSS rule. This is what you do with the bold case:To add a CSS class to the list of classes for a node, get the list of the node's CSS classes with
getStyleClass()
, and manipulate it.You have to be a little careful here, as the list can contain multiple copies of the same value, and additionally you have no control over how many times
updateItem()
is called and with whichDonation
s as a parameter. The best option is to remove all instances of the classdelete-row
and add one back in under the correct conditions:Another option is to use a CSS pseudoclass instead:
with
I would probably refactor the
NEW_DONATION
style as a pseudoclass as well in this scenario, for consistency.Here's a complete example using pseudoclasses. Note that I changed the CSS for bold (as I understand it, using
font-weight
depends on the system having a bold font for the currently-selected font; using something generic (sans-serif
) with a-fx-font
rule is more robust.)Donation.java
App.java
style.css:
Update:
If the property determining the style of the table row is not being observed by one of the columns (e.g. in the above example, the "action" column is not present), you need to arrange for the row to observe that property itself. This is a little tricky, as the row is reused for different table items, so you need to add and remove the listener from the correct property when that happens. This looks like: