java - How do I dynamically change the background of an item in a listview in JavaFX -


i writing program places set of items in listview. checks if finds items in database. if item can't found in database want change background of item in listview. using javafx program.

how do this?

you can use custom cell factory listview checks condition , applies appropriate css style class each item/cell.

the following code shows how go listview items of type string.

     listview.setcellfactory(new callback<listview<string>, listcell<string>>(){          @override         public listcell<string> call(listview<string> p) {              listcell<string> cell = new listcell<string>(){                  @override                 protected void updateitem(string t, boolean bln) {                     super.updateitem(t, bln);                     if (t != null ) {                         settext( t);                          if (item_is_not_available){                              if (!getstyleclass().contains("mystyleclass") {                                 getstyleclass().add("mystyleclass");                             }                          } else {                             getstyleclass().remove("mystyleclass");                         }                     } else {                         settext("");                     }                 }              };              return cell;         }     }); 

in css file, possible definition of mystyleclass (displaying items not available red background):

.mystyleclass{     -fx-background-color: #ff0000; } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -