title: How to easy customize JavaFX ListCell background color while selecting and unselecting cells
body: <p>I have the following problem:</p>
  
  <p>I have <code>ListView&lt;ViewsRecord&gt;</code> where <code>ViewsRecord</code> has property <code>int favorites</code>.
  If <code>favorites &gt; 0</code> it's in favorites, and if <code>favorites == 0</code> it's a regular row (not favorite).</p>
  
  <p>Now what I want to do is:</p>
  
  <ol>
  <li>When user selects cells (in multiple select mode) those cells will have default selected background (like: <code>-fx-background-color: -fx-selection-bar</code>) <br></li>
  <li><p>When cells aren't selected by the user:</p>
  
  <p>2.1. if cell isn't in favorites it has regular bacground (ex. white)</p>
  
  <p>2.2. if cell is in favorites it has green background</p></li>
  </ol>
  
  <p>So far I came up with this solution, but it's ugly code, and I wonder if there's easiest way to do that. 
  Further more, I had to use <code>setUserData()</code> to check if cell should be selected or not, otherwise during the list scroll, or select - cells had randomly changed their colors. (I assume it's because reusable objects are stored in memory and updateItem() isn't always fired).</p>
  
  <p>Here's my code:</p>
  
  <pre><code>list.setCellFactory(new Callback&lt;ListView&lt;ViewsRecord&gt;, ListCell&lt;ViewsRecord&gt;&gt;(){
              @Override
              public ListCell&lt;ViewsRecord&gt; call(ListView&lt;ViewsRecord&gt; param) {
                  ListCell&lt;ViewsRecord&gt; cell = new ListCell&lt;ViewsRecord&gt;(){
                           @Override
                          protected void updateItem(ViewsRecord item, boolean empty) {
                              super.updateItem(item, empty);
                              if(!empty){
                                  if(item.getFavorites() &gt; 0){  //favorite view
                                      if(!isSelected()){
                                          setStyle("-fx-background-color: darkseagreen;");
                                      }
                                      setUserData(new Integer(1));
                                  } else {  //normal view
                                      if(!isSelected()){
                                          setStyle("-fx-background-color: white;");
                                      }
                                      setUserData(new Integer(0));
                                  }
                                  setText(item.toString());
                              } else { //empty view
                                  setText(null); 
                                  setStyle("-fx-background-color: white;");
                                  setUserData(new Integer(0)); 
                              }
                          }
                      };
                  //fix bacground color when cell is selected
                  cell.selectedProperty().addListener( (obsVal, oldVal, newVal) -&gt; {
                      if(newVal){
                          cell.setStyle("-fx-background-color: -fx-selection-bar;");
                      } else {
                          if((Integer)cell.getUserData() == 1){ //favorite
                              cell.setStyle("-fx-background-color: darkseagreen;");
                          } else { //normal
                              cell.setStyle("-fx-background-color: white;");
                          }
                      }
                  });
                  return cell;
              }   
          });
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks to <strong>jns</strong> I've managed to simplify the code.
  Current version:</p>
  
  <pre><code>list.setCellFactory(new Callback&lt;ListView&lt;ViewsRecord&gt;, ListCell&lt;ViewsRecord&gt;&gt;(){   
              @Override
              public ListCell&lt;ViewsRecord&gt; call(ListView&lt;ViewsRecord&gt; param) {
                  final PseudoClass FAVORITE_PSEUDO_CLASS = PseudoClass.getPseudoClass("favorite");
                  ListCell&lt;ViewsRecord&gt; cell = new ListCell&lt;ViewsRecord&gt;(){
                           @Override
                          protected void updateItem(ViewsRecord item, boolean empty) {
                              if(!empty){
                                  //favorite or not, and not selected
                                  pseudoClassStateChanged(FAVORITE_PSEUDO_CLASS, (item.getFavorites() &gt; 0) &amp;&amp; !isSelected());
                                  setText(item.toString());
                              } else { 
                                  setText(null); 
                                  //empty
                                  pseudoClassStateChanged(FAVORITE_PSEUDO_CLASS, false);
                              }
                              super.updateItem(item, empty);
                           }
                  };
                  cell.selectedProperty().addListener( (obsVal, oldVal, newVal) -&gt; {
                      if(newVal){
                          //selected
                          cell.pseudoClassStateChanged(FAVORITE_PSEUDO_CLASS, false); 
                      } else {
                          //favorite or not
                          cell.pseudoClassStateChanged(FAVORITE_PSEUDO_CLASS, cell.getItem().getFavorites() &gt; 0); 
                      }
                  });
                  return cell;
              }
          });
  </code></pre>