JavaFX Tableview make specific cells colored

只愿长相守 提交于 2019-12-25 09:01:07

问题


TableColumn tc = new TableColumn();

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

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


回答1:


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:



来源:https://stackoverflow.com/questions/39763053/javafx-tableview-make-specific-cells-colored

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