Andy
Andy

Reputation: 314

JavaFX TableColumn wrong font color when using Text Class and selecting rows

Following Scenario: There are rows for the following table/columns, which have long string values. I figured out how to solve the problem with wrapping these long string values by calling the "setCellFactory" manually and binding the "text.wrappingWidthProperty()" to the column's width.

That is working fine.

This is the code snippet I was talking:

@FXML
  private TableView<Row> tableEssen;
@FXML
 private TableColumn<Row, String> columnEssen;
...
columnEssen.setCellFactory(new Callback<TableColumn<Row,String>, TableCell<Row,String>>() {
            @Override
            public TableCell<Row, String> call( TableColumn<Row, String> param) {
                 final TableCell<Row, String> cell = new TableCell<Row, String>() {
                      private Text text;
                      @Override
                      public void updateItem(String item, boolean empty) {
                           super.updateItem(item, empty);
                           if (!isEmpty()) {
                                text = new Text(item.toString());
                                text.wrappingWidthProperty().bind(getTableColumn().widthProperty());
                                text.fontProperty().bind(fontProperty());
                                setGraphic(text);
                           }
                      }
                 };
                 return cell;
            }
       });

Now my next little issue is, that the font color of the selected row is black - and it should be white. The column "Essen" works with the class Text and "Preis" is a simple string. The "Preis" column is okay, but "Essen" is not. I tried "text.fontProperty().bind(fontProperty());", but that didn't help.

enter image description here

Does anyone has an idea how to get the correct font color when selecting rows?

Edit: Problem solved.

Here the slight difference of the fonts:

left: without text.getStyleClass().addAll("table-text", "text");

right: with text.getStyleClass().addAll("table-text", "text");

without line of code with line of code

Upvotes: 1

Views: 928

Answers (1)

James_D
James_D

Reputation: 209299

The default TableCell uses a Label to display the text. In the default stylesheet, the label has its -fx-text-fill set to -fx-text-background-color. This value is a looked-up color which uses a ladder, so that the text is white on dark backgrounds, black on medium backgrounds (45% < brightness < 60%) and gray on light backgrounds. To make the text in your custom TableCell the same color, you just need to set the -fx-fill property of the text to the same looked-up color. See the code for the default style sheet (modena.css in Java 8 or caspian.css if you are still using JavaFX 2.2), which you can extract from the jfxrt.jar file.

You can set the correct color of your text as follows:

Add a style class to your Text:

text = new Text(item.toString());
text.getStyleClass().add("table-text");

and then in an external style sheet do

.table-text {
  -fx-fill : -fx-text-background-color ;
}

(You can also just set an inline style on the text:

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

but it's better practice to factor the style out into a separate stylesheet.)

Upvotes: 2

Related Questions