JavaFX Tableview使特定单元格着色

Jim*_*Lee 0 css javafx css-specificity tableview tablecolumn

TableColumn tc = new TableColumn();

tc.getStyleClass.add(".style in css file")
Run Code Online (Sandbox Code Playgroud)

我用css文件设置tablecolumn.我想让每个细胞都有不同的背景.有什么办法吗?

tableColumn row 1 bakcground color = green,row2 = red,row3 = blue ....等

GVA*_*Art 5

您必须为TableView使用setRowFactory并更改行样式.那里有一个小例子:

tableView.setRowFactory(new Callback<TableView<Data_type>, TableRow<Data_type>>(){
            //There can define some colors.
            int color = 0;
            String colors[] = new String[]{"red","blue","green"};
            @Override
            public TableRow<Data_type> call(TableView<Data_type> param) {
                final TableRow<Data_type> row = new TableRow<Data_type>() {
                    @Override
                    protected void updateItem(Data_type item, boolean empty) {
                        super.updateItem(item, empty);
                        //there write your code to stylize row
                        if(getIndex() > -1){
                            String color = colors[getIndex() % 3];
                            setStyle("-fx-background-color: "+ color + ";");

                        }
                    }
                };
                return row;
            }
        });
Run Code Online (Sandbox Code Playgroud)

结果:
在此输入图像描述