简体   繁体   中英

JTable TableCellRenderer not coloring correctly

I have created a simple JTable with my custom DefaultTableCellRenderer . On its own it is working fine (coloring last column). But as soon as I select a row OR filter/unfilter it, the row is colored, even if it shouldn't be colored at all.

My renderer:

public class StatusCellRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int col) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                table.convertRowIndexToModel(row), col);
        DataTableModel model = (DataTableModel) table.getModel();
        String data = model.getValueAt(table.convertRowIndexToModel(row), col).toString();
        if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(Color.GREEN);
        }
        if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(new Color(255, 51, 51));
        }
        return c;
    }
}

How it initially looks (and how it always should look):

在此处输入图片说明

And after selecting 2 rows (the top and bottom one):

在此处输入图片说明

As you can see, there are a few rows GREEN, which shouldn't be colored at all. Whats even more disturbing is the fact that I only selected the top and bottom row of the green block, which means that it automatically also colors the rows in between.

How can I stop this behavior and only color the rows as shown in the first picture?


The accepted answer helped me very much to overcome the issues and this is the final code:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int col) {
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
            table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col));
    DataTableModel model = (DataTableModel) table.getModel();
    String data = model.getValueAt(table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col))
            .toString();
    if (!isSelected) {
        if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(Color.GREEN);
        } else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(new Color(255, 51, 51));
        } else {
            c.setBackground(Color.WHITE);
        }
    } else {
        c.setBackground(c.getBackground());
    }
    return c;
}

It colors blue if the cell is selected and if not, then it colors WHITE, GREEN or RED depending on the value

Since the renderer component will be re-used, consider setting a default color when no condition match :

    if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
        c.setBackground(Color.GREEN);
    }
    else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
        c.setBackground(new Color(255, 51, 51));
    }
    else {
        c.setBackground(Color.GRAY.brighter());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM