java - Change TableRow color based on content -


i want make table changes row color based on row's content. therefore use following code. did myself , seems cause of problem ;) because part row background set seems not work intended:

not row goodfriends changes it's color 1 above. see placed system.out.println(row.getitem()); before color set sure row right 'row'. - , is. @ point have no idea why wrong row colored while row reference @ runtime (the right one).

any ideas?

persontable.setrowfactory(new callback<tableview<person>, tablerow<person>>() {          person person;         int counter = 0;          @override         public tablerow<person> call(tableview<person> tableview) {             tablerow<person> row = new tablerow<>();              if (counter < tableview.getitems().size() && (person = tableview.getitems().get(counter)) != null) {                 row.setitem(person);                 counter++;             }              if (row.getitem() != null && row.getitem().getstate() == state.goodfriend) {                 system.out.println(row.getitem());                 row.setstyle("-fx-background-color: green;");                 //or                 row.setbackground(new background(                         new backgroundfill(color.lightgreen, cornerradii.empty, new insets(3, 0, 3, 0))));             }              row.setonmouseclicked....          return row;     } 

update: james_d solution works fine coloring rows, sedonmouseclicked event doesn't work, because based on empty row objects.

    row.setonmouseclicked(mouseevent -> {             if (!row.isempty()) {                 defriend_button.setdisable(false);                 if (row.getitem().getstate() == state.friend) {                     goodfriend_button.setdisable(false);                 } else {                     goodfriend_button.setdisable(true);                 }             } else if (row.isempty()) {                 persontable.getselectionmodel().select(null);                 defriend_button.setdisable(true);                 goodfriend_button.setdisable(true);             }              if (mouseevent.getclickcount() == 2 && (!row.isempty())) {                 controller.showfrienddescriptionview(row.getitem());             }         }); 

you seem assuming single table row created each item in table, , in order items exist. there no such guarantee, , in fact in cases won't happen. tablerows created each visible row (whether or not contain item), , reused display different items user scrolls or items displayed change other reasons.

so need override updateitem(...) method , perform logic there.

persontable.setrowfactory(tv -> {     tablerow<person> row = new tablerow<person>() {         @override         public void updateitem(person person, boolean empty) {             super.updateitem(person, empty);             if (person != null && person.getstate() == state.goodfriend) {                 setstyle("-fx-background-color: green;");             } else {                 setstyle("");             }         }     };     row.setonmouseclicked(...);     return row ; }); 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -