JavaFX Tableview使特定单元格彩色 [英] JavaFX Tableview make specific cells colored

查看:361
本文介绍了JavaFX Tableview使特定单元格彩色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TableColumn tc = new TableColumn();

tc.getStyleClass.add(".style in css file")

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

i set up the tablecolumn with css file. and i want to make each cell has different backgrounds. is there any way to do it?

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

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

推荐答案

您必须对TableView使用setRowFactory并更改行样式。
有一个例子:

You have to use setRowFactory for your TableView and change row style. A little example there:

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

结果:

Result:

这篇关于JavaFX Tableview使特定单元格彩色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆