Benj Benj - 3 months ago 39
CSS Question

Change css for a new row added in TableView Javafx

I'm sorry for mistake I'm french.
So I have a tableView empty. I have a button "Add" when on click added row in a tableView. And when I select an row in my tableView, a new button "Cancel" show.
And when I click on a button "Cancel", the row's css change on my row selected (added a class css ".cancel").

The problem is that I click on button "Cancel", and after I click in the button "Add", the css ".cancel" is applicated at an other row while I don't clicked in the button "Add".

I think that there is a problem in index row.

In my method initialize :

articleTable.setRowFactory(param -> new TableRow<LigneTicket>() {
protected void updateItem(LigneTicket paramT, boolean empty) {
super.updateItem(paramT, empty);
if (!isEmpty() && paramT != null && paramT.getArticle().isArticleCanceled()) {

my code on button "Cancel" :

public void cancelLigneTicket() {
int indexSelected = articleTable.getSelectionModel().getSelectedIndex();


List<LigneTicket> items = new ArrayList<>(articleTable.getItems());



Help !!



TableRows are used to display the table items. That doesn't mean however, that it will be used with only one item.

This can result in the following sequence of events for a row r:

  1. The item of r is updated to a canceled item and thus the cancel CSS class is added.
  2. The item of r is updated to a non-canceled item, but the cancel CSS class is not removed.

You need to remove the class again. Furthermore with your code the style class could be added multiple times leading to unnecessary memory consumption.

boolean canceled = !empty && paramT != null && paramT.getArticle().isArticleCanceled());
if (canceled) {
     if (!getStyleClass().contains("cancel"))
} else {

or using PseudoClass:

private static final PseudoClass CANCELED = PseudoClass.getPseudoClass("cancel");

pseudoClassStateChanged(CANCELED, !empty && paramT != null && paramT.getArticle().isArticleCanceled());

Furthermore you should prefer the TableView.refresh (available in JavaFX >= 8u60) method to refresh the cell items instead of copying the list and setting the items.