Java 设置具有相同日期的行的背景色

Java 设置具有相同日期的行的背景色,java,swing,background-color,Java,Swing,Background Color,我需要设置行的背景色。例如,有以下行: 29.12.14 Absences 12.01.15 Absences 12.01.15 Accounts 现在,如果有两行具有相同的日期(12.01.15以上),那么它们在GUI中应该具有相同的背景颜色 String week = new String(); int weekCounter = 0; int colour = 0; // Append the rows for (ExcelRow row

我需要设置行的背景色。例如,有以下行:

29.12.14  Absences
12.01.15  Absences
12.01.15  Accounts
现在,如果有两行具有相同的日期(12.01.15以上),那么它们在GUI中应该具有相同的背景颜色

    String week = new String();
    int weekCounter = 0;
    int colour = 0;

    // Append the rows
    for (ExcelRow row : printOutRows) {
        if (weekCounter == 0){
        week = row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString();
            colour = 0;
        }
        else if (row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString().equals(week)){
            colour = 0;
        }
        else {
            week = row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString();
            colour = 1;
        }

        model.addRow(new Object[] {
                row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString(),
                row.getExcelCells().get(1).getExcelCell().getRichStringCellValue().getString(),
                row.getExcelCells().get(2).getExcelCell().getRichStringCellValue().getString(),
                row.getExcelCells().get(3).getExcelCell().getNumericCellValue()});  

        if (colour == 0){
            table.setSelectionBackground(Color.RED);
        }
        else{ 
            table.setSelectionBackground(Color.LIGHT_GRAY);
        }
        weekCounter ++;

    }

我尝试了上面的代码,但是JTable中的所有行都有白色背景。如何实现目标?

在第一列中添加颜色变量即可。在我的示例中,我使用了一个布尔值,但如果您使用其他任何东西,它也可以工作

在第一部分中,我准备了您的代码,就像您以前做的一样,但首先是颜色。
之后,我从视图中删除第一列,这样您就看不到它了。但它仍然存在。
最后,根据前面设置的变量呈现输出

String week = "";
boolean colorSwitch = false;

for (ExcelRow row : printOutRows) {
    if (!row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString().equals(week)){
        week = row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString();
        colorSwitch = !colorSwitch;
    }

    model.addRow(new Object[] {
        colorSwitch,
        row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString(),
        row.getExcelCells().get(1).getExcelCell().getRichStringCellValue().getString(),
        row.getExcelCells().get(2).getExcelCell().getRichStringCellValue().getString(),
        row.getExcelCells().get(3).getExcelCell().getNumericCellValue()
   });  

}

// remove first column from the view (the one with the boolean value in it)      
TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(0) );

// render the table according to the color.      
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

        if ((Boolean) table.getModel().getValueAt(row, 0)) {
            setBackground(Color.BLACK);
            setForeground(Color.WHITE);
        } else {
            setBackground(table.getBackground());
            setForeground(table.getForeground());
        }       

        return this;
    }   
  });

选择背景可能不是一个好办法。尝试为有问题的行设置单元格背景。您可以将逻辑放入TableCellRenderer,然后将渲染器设置到表中。setBackground(c色)应该在渲染器中起作用。请阅读有关。和