3

I need to set the background color of my rows. For example there are these rows:

29.12.14  Absences
12.01.15  Absences
12.01.15  Accounts

Now if there are 2 rows with the same date (above 12.01.15) they should have the same background color in the 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 ++;

    }

I tried the code above, but all rows in the JTable are having a white background. How can I reach my goal?

Phil
  • 87
  • 1
  • 1
  • 9
  • 2
    Selection background is probably not the way to go. Try to set the cell background for the rows in question. – Thomas Mar 22 '16 at 09:20
  • You can put your logic in a TableCellRenderer and set the renderer into the table. setBackground(Color c) should do the trick in renderer. – Endery Mar 22 '16 at 09:21
  • 1
    Please read about [Renderer/Editor concept in Table](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) – Sergiy Medvynskyy Mar 22 '16 at 09:22
  • 1
    For [example](http://stackoverflow.com/a/5799016/230513). – trashgod Mar 22 '16 at 09:44
  • 1
    [Concepts: Editors and Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) and [Using Custom Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer) – MadProgrammer Mar 22 '16 at 10:20

1 Answers1

1

You can do it when you add a color variable in the first column. In my example I used a Boolean, but it would work as well if you'd use anything else.

In the first part I prepare your code, as you did before, but at as first the color.
After that I remove the first column from the view, so you don't see it. But it is still there.
Lastly I render the output accourding to the variable we set earlier.

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;
    }   
  });
arc
  • 4,553
  • 5
  • 34
  • 43