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

两盒软妹~` 提交于 2020-01-05 07:14:12

问题


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);

回答1:


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

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

The result is:

Beside that you can change the alternating color with:

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


来源:https://stackoverflow.com/questions/55342984/change-tablecolumn-background-in-tableview-whilst-retaining-alternating-row-col

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!