JavaFX, change css tablecell on sort

304 views Asked by At

I have to change the styleclass of a TableCell in function of the data displayed, for example: if the value of a cell is the same in two contiguous rows, the cell has to be highlighted (i.e.: background red). This must work both adding data to the table and sorting by any column.

To do so, on sorting, I added a listener to tableview.getSortOrder() and in its onChange method the logic to do what I described above. By example:

public void onChanged(ListChangeListener.Change<? extends TableColumn> change) {
   if (change.getList().size() > 0) {
      Platform.runLater(new Runnable() {
         @Override
         public void run() {
            /* set some css on tableview cells */
         }
      });
   }
}

The problem is that doing so css changes are applied on the next refresh of the table not immediately. My suspects are on doing this inside Platform.runLater but I need it to have the data sorted as they should be, before changing styles.

Did I do anything wrong? Does exist a better way to do what I described?

1

There are 1 answers

1
WonderWorld On

You could do that if you have a specific css file for it and add it.

final String css = getClass.getResource("the_css.css").toExternalForm();

and add this in the Platform.runLater:

 scene.getStylesheet.add(css);

remove if necessary with:

scene.getStylesheet.remove(css);

Not exactly sure if this does the trick, but it should change the background color right away and not after a refresh. i hope it's helping you in the right direction.