Georgian
Georgian

Reputation: 8960

TableView -fx-text-fill on rows

Acording to this very similar question, either high specificity or the !important tag should be used to change very 'hidden' selectors.

However, I cannot seem to change the -fx-text-fill of a TableRow.

I'd rather solve this issue than using setTextFill in the row factory.


SSCCE

The tableviewtester.css goes next to the TableViewTester class, and contains:

.myrow {
    -fx-selection-bar-text: red;
    -fx-text-fill: red; 
}

Notice the -fx-selection-bar-text working.

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * 
 * @author ggrec
 *
 */
public class TableViewTester extends Application
{

    // ==================== 3. Static Methods ====================

    public static void main(final String[] args)
    {
        launch(args);
    }


    // ==================== 4. Constructors ====================

    @Override
    public void start(final Stage primaryStage) throws Exception
    {
        final Scene scene = new Scene(createContents());
        scene.getStylesheets().add(this.getClass().getResource("tableviewtester.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.show();
    }


    // ==================== 5. Creators ====================

    private StackPane createContents()
    {
        final StackPane pane = new StackPane();

        final TableView<String> table = new TableView<>();
        table.setRowFactory(new TableRowFactory());

        final TableColumn<String, String> column = new TableColumn<>("Color");
        column.setCellValueFactory(new TableCellValueFactory());
        column.prefWidthProperty().setValue(300);

        // Add Columns
        table.getColumns().setAll(FXCollections.observableArrayList(column));

        // Add Items
        table.setItems(FXCollections.observableArrayList("Green", "Red", "Blue"));

        pane.getChildren().setAll(table);

        return pane;
    }


    // =======================================================
    //           19. Inline Classes 
    // =======================================================

    private class TableCellValueFactory implements Callback<CellDataFeatures<String, String>, ObservableValue<String>>
    {
        @Override
        public ObservableValue<String> call(final CellDataFeatures<String, String> cellWrapper)
        {
            return new SimpleStringProperty(cellWrapper.getValue());
        }
    }

    private class TableRowFactory implements Callback<TableView<String>, TableRow<String>>
    {
        @Override
        public TableRow<String> call(final TableView<String> arg0)
        {
            final TableRow<String> row = new TableRow<String>() {

                @Override protected void updateItem(final String line, final boolean empty)
                {
                    super.updateItem(line, empty);

                    this.getStyleClass().add("myrow");
                }
            };

            return row;
        }
    }

}

Upvotes: 1

Views: 1801

Answers (1)

Georgian
Georgian

Reputation: 8960

-fx-text-inner-color is the correct attribute, instead of -fx-fill-color.


Reference #1

Reference #2

Reference #3

Reference #4

Upvotes: 1

Related Questions