1

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>() {
        @Override
        protected void updateItem(LigneTicket paramT, boolean empty) {
            super.updateItem(paramT, empty);
            if (!isEmpty() && paramT != null && paramT.getArticle().isArticleCanceled()) {
                getStyleClass().add("cancel");
            }
        }
    });

my code on button "Cancel" :

    public void cancelLigneTicket() {
    int indexSelected = articleTable.getSelectionModel().getSelectedIndex();
                     articleTable.getItems().get(indexSelected).getArticle().setArticleAnnuler(true);

articleTable.getSelectionModel().clearSelection();

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

articleTable.getItems().setAll(items);

buttonAnnulArt.setVisible(false);

Help !!

Thanks.

Benj
  • 279
  • 6
  • 20
  • Do you ever remove the style class `cancel`? Also - you are probably better off using Pseudo Class, see http://stackoverflow.com/questions/37072514/how-to-add-class-or-pseudoclass-programmatically-to-custom-control-in-javafx and http://stackoverflow.com/questions/36883838/implement-read-only-style-in-javafx/36886291#36886291 – Itai Jul 05 '16 at 12:00
  • No I don't remove the class "cancel" because every row selected and after clicked in the button "Cancel" must keep the class css "cancel". I have replace with the code "pseudoClassStateChanged(PseudoClass.getPseudoClass("cancel"), true);", but I don't work... – Benj Jul 05 '16 at 12:09
  • I'm not sure I understand that you mean, but keep in mind that `TableView` re-uses rows, so if you have set a property on the row object, you have to make sure it is still valid with every `updateItem` - the same row may now be used for an item that wasn't cancelled! Also - what do you mean by they pseudo class not working? Same problem, or not changing style at all? Keep in mind pseudo classes have to be specified by : (colon) in the css, not . (period), so it should be ":cancel" rather than ".cancel" . – Itai Jul 05 '16 at 12:21
  • I changed in my css by ":cancel" and the css work, but the problem is always the same. If I click in button "Add" after the button "Cancel", the css is not applicated in the same row... The program applicate the css at an other row, and I don't click in the button "cancel". – Benj Jul 05 '16 at 12:32

1 Answers1

1

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"))
          getStyleClass().add("cancel");
} else {
     getStyleClass().remove("cancel");
}

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.

fabian
  • 80,457
  • 12
  • 86
  • 114