ListView styling in JavaFX

fxmljavafxjavafx-8listviewstyling

I have a listview. I want to change the color of the background and the text fill. I have tried doing the following:

playlistView.setStyle("-fx-background-color: blue; -fx-text-fill: black;");

But, this doesn't work. However, the following is working:

playlistView.setStyle("-fx-font-size: 24px; -fx-font-family: 'SketchFlow Print';");

Can you please tell me how to get the background and text-fill to work? Thank you.

Best Answer

You can use the following CSS styles:

.list-view .list-cell:even {
-fx-background-color: blue;
-fx-text-fill: black;
}
.list-view .list-cell:odd {
-fx-background-color: blue;
-fx-text-fill: black;
}
  1. Save the content of this style in a lisStyles.css.
  2. Add the URL for the lisStyles.css style sheet to the scene:

    scene.getStylesheets().add(getClass().getResource("lisStyles.css").toExternalForm());