JTable: How do I paint a cell red? or green?

How do I set the background color of a cell in a JTable?
Who is Participating?
manuel_mConnect With a Mentor Commented:
2 solutions:

1. In your subclass of JTable include the code looking something like this:

 public Component prepareRenderer(TableCellRenderer renderer,
                                         int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
            } else {
                // If not shaded, match the table's background
            return c;

This will color every second column red.
Advantage: Works for every kind of cell renderer (Text, number, date,
custom renderes etc.).
Disadvantage: You have to subclass JTable

2. Make your own version of a TableCellRenderer and put the logic in

Advantage: You do not need to subclass JTable
Disadvantage: It will only change the color for those cells which have
your cellrenderer. e.g. you need to manually install them.
ycompAuthor Commented:
Thanks sudhakar, unfortunately I just accept manuel's answer (his was good too). Should have split the points but I didn't see yours until after I accepted.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Here is thre model to writeCellREnderrer class

 JTable table = new JTable();
    // Add data...
    // Install the custom renderer on the first visible column
    int vColIndex = 0;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellRenderer(new MyTableCellRenderer());
    // This renderer extends a component. It is used each time a
    // cell must be displayed.
    public class MyTableCellRenderer extends JLabel implements TableCellRenderer {
        // This method is called each time a cell in a column
        // using this renderer needs to be rendered.
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
            // 'value' is value contained in the cell located at
            // (rowIndex, vColIndex)
            if (isSelected) {
                // cell (and perhaps other cells) are selected
            if (hasFocus) {
                // this cell is the anchor and the table has the focus
            // Configure the component with the specified value
            // Set tool tip if desired
            // Since the renderer is a component, return itself
            return this;
        // The following methods override the defaults for performance reasons
        public void validate() {}
        public void revalidate() {}
        protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
        public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
Oh, I am late. No problems ;-)
All Courses

From novice to tech pro — start learning today.