0

I have the following code , where based on a variable value i change font color of a column to red.

public class TableViewExample extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        TableView tableView = new TableView();

        tableView.getStylesheets().add(TableViewExample.class.getResource("TableEditor.css").toExternalForm());

        TableColumn<Person, String> column1 = new TableColumn<>("First Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
        column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        column2.setCellFactory((TableColumn<Person, String> param) -> {
            TableCell<Person, String> cell = new TableCell<Person, String>() {
                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty && getTableRow() != null && getTableRow().getItem() != null) {
                        if (((Person) getTableRow().getItem()).isShowRed()) {
                            setTextFill(Color.RED);
                        } else {
                            setTextFill(Color.BLACK);
                        }
                        setText(item);
                    }
                }
            };
            return cell;
        });

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);

        tableView.getItems().add(new Person("John", "Doe", false));
        tableView.getItems().add(new Person("Jane", "Deer", true));
        tableView.getItems().add(new Person("John", "Doe", false));
        tableView.getItems().add(new Person("Jane", "Deer", true));
        tableView.getItems().add(new Person("John", "Doe", false));
        tableView.getItems().add(new Person("Jane", "Deer", true));

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public class Person {

        private String firstName = null;
        private String lastName = null;
        private boolean showRed = false;

        public Person() {
        }

        public Person(String firstName, String lastName, boolean red) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.showRed = red;
        }

        public boolean isShowRed() {
            return showRed;
        }

        public void setShowRed(boolean showRed) {
            this.showRed = showRed;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }

}

TableEditor.css

.table-view:focused .table-row-cell:filled:focused:selected {
    -fx-background-color: blue;
    -fx-text-fill: yellow;
}

/* Row hovered */
.table-view .table-row-cell:filled:hover {
    -fx-background-color: blue;
    -fx-text-background-color: yellow;
}

The problem is , if i do setTextFill for column2, the hover and Selection font colors from css are not working for the column2 alone.

I understand its because of the precedence of inline styling.

But how to overcome that, the column cell should be in red color, on hover and selection it should take color from css.

user3164187
  • 1,382
  • 3
  • 19
  • 50
  • 4
    Why not do everything within the stylesheet? Why the calls to `setTextFill` in code? – Slaw Aug 09 '21 at 06:21
  • 1
    Related: [How to set cell background color (JavaFX, tableview)](https://stackoverflow.com/questions/41362827/how-to-set-cell-background-color-javafx-tableview), and also [JavaFX TableView row highlighting sample using table row apis and css.](https://gist.github.com/jewelsea/5470095) – jewelsea Aug 09 '21 at 21:44

0 Answers0