1

i am getting only last row coloured..

class CustomTableCellRenderer extends DefaultTableCellRenderer {

    private Map<Integer, Color> mapColors;

    public CustomTableCellRenderer() {
        mapColors = new HashMap<>();
    }

    public void setRowColor(int row, Color color) {
//        System.out.println(row + "...happy...." + color);
        mapColors.put(row, color);

    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
        Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1);
        Color color = mapColors.get(row);
        if (color != null) {
            cell.setBackground(color);
        } else {
            cell.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        }
        row++;
        return cell;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Akarsha
  • 29
  • 2
  • 7
  • A cell rendered is applied for ALL cells in a given column. If you want a particular cell to be customized, you will need to provide some kind of look up in order to provide that functionality – MadProgrammer Jan 22 '15 at 05:58
  • This seems to be a duplicate of http://stackoverflow.com/questions/7181699/changing-swing-jtable-cell-colors. Take a look at the provided answer. – Kalenda Jan 22 '15 at 06:02

1 Answers1

6

A cell renderer is a shared resource which is applied to ALL the cells for a given column. In order to provide customisation for a given row or rows, you need to provide a means by which you can remember which row you want and it's color, for example...

public class CustomTableCellRenderer extends DefaultTableCellRenderer {

    private Map<Integer, Color> mapColors;

    public CustomTableCellRenderer() {
        mapColors = new HashMap<Integer, Color>();
    }

    public void setRowColor(int row, Color color) {
        mapColors.put(row, color);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

        Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1);
        Color color = mapColors.get(row);
        if (color != null) {
            cell.setBackground(color);
        } else {
            cell.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        }
        return cell;
    }
}

For example...

Rows

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DefaultTableModel model = new DefaultTableModel(new Object[]{"One"}, 6);
                CustomTableCellRenderer renderer = new CustomTableCellRenderer();
                renderer.setRowColor(0, Color.RED);
                renderer.setRowColor(1, Color.GREEN);
                renderer.setRowColor(2, Color.BLUE);
                renderer.setRowColor(3, Color.PINK);
                renderer.setRowColor(4, Color.MAGENTA);
                JTable table = new JTable(model);
                table.getColumnModel().getColumn(0).setCellRenderer(renderer);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    class CustomTableCellRenderer extends DefaultTableCellRenderer {

        private Map<Integer, Color> mapColors;

        public CustomTableCellRenderer() {
            mapColors = new HashMap<Integer, Color>();
        }

        public void setRowColor(int row, Color color) {
            mapColors.put(row, color);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1);
            Color color = mapColors.get(row);
            if (color != null) {
                cell.setBackground(color);
            } else {
                cell.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
            }
            return cell;
        }
    }
}

See Concepts: Editors and Renderers for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366