0

I am trying to override foreground color for table rows with something like this

.table-row-cell:odd {
    -fx-text-fill: blue;
    -fx-background-color: lightblue;
}

.table-row-cell:even {
    -fx-text-fill: red;
    -fx-background-color: lightpink;
}

I see, that background color works, while foreground does not:

enter image description here

The sample code is follows:

**
 * Checking how to override foreground color
 */
public class TableViewCSS2 extends Application {

   private static String[][] names = new String[][]{
      {"Julius", "Caesar"},
      {"Wolfgang", "Mozart"},
      {"William", "Shakespeare"},
      {"Napoleon", "Bonaparte"},
      {"Isaac", "Newton"},
      {"Albert", "Einstein"},
      {"Christopher", "Columbus"},
      {"Johann", "Bach"},
      {"Ludwig", "Beethoven"},
      {"Gautama", "Buddha"},
      {"Martin", "Luther"},
      {"Galileo", "Galilei"},
      {"Karl", "Marx"},
      {"Marco", "Polo"},
      {"Immanuel", "Kant"}
   };




   BorderPane root;
   {
      BorderPane ans = new BorderPane();
      root = ans;
   }


   TableView<String[]> table;
   {
      TableView<String[]> ans = new TableView<String[]>();
      root.setCenter(ans);

      table = ans;
   }

   TableColumn<String[],String> firstNameCol;
   {
      TableColumn<String[],String> ans = new TableColumn<String[], String>("First Name");
      ans.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String[], String>, ObservableValue<String>>() {
         public ObservableValue<String> call(final TableColumn.CellDataFeatures<String[], String> param) {
            return new ObservableValueBase<String>() {
               public String getValue() {
                  return param.getValue()[0];
               }
            };
         }
      });
      table.getColumns().add(ans);

      firstNameCol = ans;
   }


   TableColumn<String[],String> secondNameCol;
   {
      TableColumn<String[],String> ans = new TableColumn<String[], String>("Second Name");
      ans.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String[], String>, ObservableValue<String>>() {
         public ObservableValue<String> call(final TableColumn.CellDataFeatures<String[], String> param) {
            return new ObservableValueBase<String>() {
               public String getValue() {
                  return param.getValue()[1];
               }
            };
         }
      });
      table.getColumns().add(ans);

      secondNameCol = ans;
   }


   private ObservableList<String[]> namesList;
   {

      ObservableList<String[]> ans = FXCollections.observableArrayList();

      for(int i=0; i<names.length; ++i) {
         ans.add( names[i] );
      }
      table.setItems(ans);

      namesList = ans;
   }



   @Override
   public void start(Stage primaryStage) throws Exception {

      Scene scene = new Scene(root, 800, 600);
      scene.getStylesheets().add("/sample2.css");

      primaryStage.setTitle("TableViewCSS2");
      primaryStage.setScene(scene);
      primaryStage.show();
   }




}

UPDATE 2

Adding important! didn't help.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 2
    What are you referring to by the foreground color? If you mean the text color use `-fx-text-fill`. You can also check the [CSS reference](http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#labeled) – hotzst May 13 '16 at 05:50
  • 2
    `.table-cell{-fx-text-fill: red;}` try this! – Rhain May 13 '16 at 08:02
  • @Rhain got it working! – Dims May 13 '16 at 08:39

2 Answers2

2

The following CSS worked:

.table-row-cell:odd {

    -fx-background-color: lightblue;
}

.table-row-cell:odd > .table-cell{
    -fx-text-fill: blue;
}

.table-row-cell:even {
    -fx-background-color: lightpink;

}

.table-row-cell:even > .table-cell{
    -fx-text-fill: red;
}
Dims
  • 47,675
  • 117
  • 331
  • 600
1

And you can check the default styles defined by JvaFX itself. See also: Default JavaFX-CSS You'll find plenty of examples in there

The selector is valid and applies to the font color. Seeing as the text is not red, it's possible that a more-specific CSS rule applies (and takes priority)

Community
  • 1
  • 1
Daan Terra
  • 73
  • 7