Setting font color of JavaFX TableView Cells?

Viewed 38379

In my Java Desktop Application I have a JavaFX Table with 3 columns. I want to set the font color of the 3rd column to red. I have not been able to set the font color of the Tableb at all. I looked into CSS and I did not find anything. Is there a way to do it with CSS? I also looked for setFont() with the hope of setting it that way. Nothing there. I could not even figure a way to set something on a certain cell.

TableView<TableData> myTable = new TableView<TableData>();
ObservableList<TableData> myTableData = FXCollections.observableArreyList(
    new TableData("data", "data", "data"),
    new TableData("data", "data", "data"));

TableColumn firstColumn = new TableColumn("First Column");
firstColumn.setProperty("one");
TableColumn secondColumn = new TableColumn("Second Column");
secondColumn .setProperty("two");
TableColumn thirdColumn = new TableColumn("Third Column");
thirdColumn .setProperty("three");

myTable.setItems(myTableData);
myTable.getColumns.addAll(firstColumn, secondColumn, thirdColumn);

How can I accomplish this? How can I set to font color? Any help will be appreciated.

5 Answers

I found a CSS code. Apply this red-column class into your column.

.red-column.table-cell {
    -fx-padding: 0.5em;
    -fx-border-color: transparent -fx-box-border transparent transparent;
    -fx-font: 13px "Arial";
    -fx-text-fill: red;
}

Your table will be like this.

enter image description here

Full css for table-view is here.

Or, similar to Panduka,

tableColumn.setStyle("-fx-text-fill: red");

For a multiline cell :

    Callback<TableColumn<MyDTO, String>, TableCell<MyDTO, String>> multilineRedCallback = param -> {
        TableCell<MyDTO, String> cell = new TableCell<MyDTO, String>();
        Text text = new Text();
        cell.setGraphic( text );
        cell.setPrefHeight( Region.USE_COMPUTED_SIZE );
        text.setFill( Color.RED );
        text.wrappingWidthProperty().bind( cell.widthProperty() );
        text.textProperty().bind( cell.itemProperty() );
        return cell;
    };
    this.colMultilineRed.setCellFactory( multilineRedCallback );
Related