5

I'm looking for a solution for this problem: I have an excel file, that contains data. Some of the cells have yellow background. I already created a code for importing the text to JTable, which works fine. But I want to import the background-cell-color to specific cells also. For simplicity-sake of this example, I didn't use loops, reading the excel data from source etc. After reading the forum I understood I need CustomCellRenderer.

I have a problem with this approach, because this code colors the cells in the column correctly at first, but when I start to scroll over the colored cells in this table, it recolors the whole column to yellow. (see the screenshot)

I thought I could add else statement to specifically color the remaining cells to white, but this approach won't work for me, because I would be overwriting my previous cell results.

Can you point me to a solution on this one? (is this a bug, or expected behavior of JTable?). I'm using NetBeans and the GUI drag n drop generator

enter image description here

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MyRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        int[][] coordinatesYellow = new int[3][2];
        //[row][column] these cells are yellow
        coordinatesYellow[0][0] = 3;
        coordinatesYellow[0][1] = 2;
        coordinatesYellow[1][0] = 4;
        coordinatesYellow[1][1] = 2;
        coordinatesYellow[2][0] = 2;
        coordinatesYellow[2][1] = 2;

        for (int i = 0; i < 3; i++) {
            if ((row == coordinatesYellow[i][0]) && (column == coordinatesYellow[i][1])) {
                c.setBackground(Color.yellow);
            }
        }
        return c;
    }
} 



// And this is the statement I use for calling the renderer:
// resultsTable.getColumnModel().getColumn(0).setCellRenderer(new MyRenderer());
Pendula
  • 688
  • 6
  • 17
radox1912
  • 53
  • 2

1 Answers1

2

Make it so that when your cell should not be yellow, that you set the background to white (or the table's background color).

A renderer that extends DefaultTableCellRenderer uses the same component (a JLabel) as a template for all cells (cf DefaultTableCellRenderer implementation notes - they call it rubber-stamping). Once you set its background to yellow, it will remain yellow for rendering consecutive cells until you change its background color again.

Replace your for loop with something like the following:

boolean isYellow = false;
for (int i = 0; i < 3; i++) {
            if ((row == coordinatesYellow[i][0]) && (column == coordinatesYellow[i][1])) {
                c.setBackground(Color.yellow);
                isYellow = true;
            }
        }
if( !isYellow )
  c.setBackground(Color.white);
TT.
  • 15,774
  • 6
  • 47
  • 88
  • This doesn't work as intended. If I add else statement into the for loop, the result is only the sampletext cell is in yellow, because the for loop overwrites the results. – radox1912 Jan 11 '16 at 15:48
  • 1
    @radox1912 In **all** cases where you don't set the color to yellow... That means if you haven't set anything to color yellow, you should set it to white. – TT. Jan 11 '16 at 15:59
  • @radox1912 I've added a code segment that reflects my statement. – TT. Jan 11 '16 at 16:04
  • I tested the solution, boolean isYellow does the trick ! Thank you very much. – radox1912 Jan 12 '16 at 09:04
  • @radox1912 You can thank me most by accepting the answer (checking the checkmark at the top left of the answer). You are welcome btw =) – TT. Jan 12 '16 at 09:05