0

I have the next function:

tableSoldCoffees.setRowFactory(typesOfCoffeesTableView -> new TableRow<TypesOfCoffees>() {
            @Override
            protected void updateItem(TypesOfCoffees item, boolean empty) {
                super.updateItem(item, empty);
                if (isSelected()) {
                    setStyle("-fx-background-color: #74512D");
                } else if (!isSelected()) {
                    setStyle("");
                }
            }
        });

Here, in the table-view, when the item is selected the function changes the bakcground of the row, but i don't know how to reach "-fx-text-fill" and change color of the text itself. If i write it inside, it will not work.

I can change in the CSS stylesheet:

.myTable .table-row-cell:selected .table-cell{
    -fx-text-fill: white;
}

But i need to do this particularly in the code

2
  • 2
    What’s wrong with the CSS approach? That is the supported way to do this. Commented May 27, 2024 at 11:47
  • 1
    I think you can only change the text color in the TableCell. Commented May 27, 2024 at 13:12

1 Answer 1

1

I agree with the comments mentioned by @James_D and @Abra.

But if you say that you are OK with adding styling in java code, then you can try the below approach. (provided that your javafx version is higher than 8.0)

final String SELECTED_CSS = "data:text/css," +
        """
           .table-cell{
               -fx-text-fill: red;
           }
        """;       
tableSoldCoffees.setRowFactory(typesOfCoffeesTableView -> new TableRow<TypesOfCoffees>() {
    @Override
    protected void updateItem(TypesOfCoffees item, boolean empty) {
        super.updateItem(item, empty);
        if (isSelected()) {
            setStyle("-fx-background-color: #74512D");
            // Update the stylesheet for the table row.
            getStylesheets().setAll(SELECTED_CSS);
        } else if (!isSelected()) {
            setStyle("");
            getStylesheets().clear();
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.