trilogy
trilogy

Reputation: 1795

Change TableColumn background in TableView, whilst retaining alternating row colour?

I was wondering if I can add a "layer" of colour on top of a column (cell background) without losing entirely the alternating row colours already present. I was using setStyle, and that just adds a solid colour.

   TableView<LineItem> table = new TableView<>();

    table.getSelectionModel().setCellSelectionEnabled(true);
    TableColumn<LineItem, String> column1 = new TableColumn<>("Test1");
    column1.setCellValueFactory(cellData -> cellData.getValue().string1Property());
    column1.setEditable(true);

    table.getColumns().add(column1);

    TableColumn<LineItem, String> column2 = new TableColumn<>("Test2");
    column2.setCellValueFactory(cellData -> cellData.getValue().string2Property());
    column2.setEditable(true);

    column2.setCellFactory(e -> new TableCell<LineItem, String>()
    {
        @Override
        public void updateItem(String item, boolean empty)
        {
            super.updateItem(item, empty);
            setStyle("-fx-background-color: green;");
            if (item == null || empty)
            {
                setText(null);
            } else
            {
                setText(item);

            }
        }
    });

    TableColumn<LineItem, String> column3 = new TableColumn<>("Test3");
    column3.setCellValueFactory(cellData -> cellData.getValue().string2Property());
    column3.setEditable(true);

Upvotes: 1

Views: 153

Answers (1)

Samuel Philipp
Samuel Philipp

Reputation: 11042

You can just use a semi-transparent color in your TableCell:

setStyle("-fx-background-color: rgba(0, 128, 0, 0.3);");

The result is:

result cells

Beside that you can change the alternating color with:

table.setStyle("-fx-control-inner-background-alt: #777777;");

Upvotes: 3

Related Questions