简体   繁体   English

JavaFx动态设置Tableview单元格背景颜色

[英]JavaFx Set Tableview Cell Background Color Dynamically

I want to add color to the color cell of the rows dynamically when ADD button is clicked. 我想在单击ADD按钮时动态地向行的颜色单元格添加颜色。 I am not able to change the background color of the cell. 我无法更改单元格的背景颜色。 Please see the image for reference. 请参阅图片以供参考。 Am not able to achieve that with the help of code. 在代码的帮助下无法实现这一点。 Thanks for help in advance. 提前感谢您的帮助。

Snippet adding values to table : 将值添加到表的代码段:

     @FXML
     private void addEntity() {

      data.add(new Inventory(codeTemp.getText(), articleNameTemp.getText(), Integer.parseInt(amountTemp.getText()), dcTemp.isSelected() ? true:false, stTemp.isSelected()?true:false, Utilities.toRGBCode(colorTemp.getValue()), informationTemp.getText(), data.size()+1));
      inventoryTable.setItems(data);

     }

在此输入图像描述

Did with the help of a callback on the column. 在列的回调的帮助下完成了。

        Callback<TableColumn<Inventory, String>, TableCell<Inventory, String>> cellFactory =
        new Callback<TableColumn<Inventory, String>, TableCell<Inventory, String>>() {
            public TableCell call(TableColumn p) {
                TableCell cell = new TableCell<Person, String>() {
                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        setText(empty ? null : getString());
                        setStyle("-fx-background-color:"+getString());
                    }

                    private String getString() {
                        return getItem() == null ? "" : getItem().toString();
                    }
                };


                return cell;
            }
        };

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM