0

I created the following TableCellRenderer to set colors of particular cells in JTable. The problem is that it sets the color of the whole column. How do I define the row?

package run;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class test4 {


    /**
     * @param args
     */
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(); 
            }
        });     
    }

    private static void createAndShowGUI() {
        gtest t = new gtest("");
        t.pack();
        t.setVisible(true);
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setLocationRelativeTo(null);
    }


}

class gtest extends JFrame
{

    private static JTable table;
    private int index;

    public gtest(String title)
    {
        Object cols[] = {"A","B"};
        double data[][] = {{2,10},{5,20},{20,11}};
        table = new JTable(3,2);

        for (int i = 0; i< data.length; i++)
        {
            for (int j=1; j<cols.length; i++)
            {
                double val = data[i][j] + 5*data[i][j]-1;
                table.getColumnModel().getColumn(j).setCellRenderer(new ColorRenderer());
                // here I want to put a Thread.sleep or something similar to
                // visualize the filling out of a table
            }
        }
        add(table);

    }
}

class ColorRenderer extends JLabel implements TableCellRenderer  {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public ColorRenderer() {
        setOpaque(true);
    }

    public Component getTableCellRendererComponent(
                            JTable table, Object value,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
      setText(value.toString());
      setFont(new Font("SansSerif", Font.PLAIN, 10));
      setBackground(Color.lightGray);
      return this;
    }
}
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • 2
    Ovderride `prepareRenderer()`, for [example](http://stackoverflow.com/a/5799016/230513). – trashgod Jan 30 '15 at 12:23
  • 1
    BTW - `new Font("SansSerif", Font.PLAIN, 10)` Don't use 'magic numbers' when there is a constant defined. That should better be `new Font(Font.SANS_SERIF, Font.PLAIN, 10)`.. – Andrew Thompson Jan 30 '15 at 14:37

1 Answers1

3

TableCellRenderer set to whole column, to change just some rows, you need to implement validation of row numbers inside getTableCellRendererComponent() method.

For example chage color of rowIndex = 1:

setBackground(row == 1 ? Color.lightGray : table.getBackground());

Read doc for TableCellRenderer.

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • Could you give an example? – Klausos Klausos Jan 30 '15 at 11:39
  • I understand that inside getTableCellRendererComponent(){...} I must check if row is equal to current row. But how can I get this current row? – Klausos Klausos Jan 30 '15 at 11:51
  • I am not sure that this works for my case. Imagine I am filling out the table inside the for loop: table.setValueAt(val,row,col); table.getColumnModel().getColumn(col).setCellRenderer(new ColorRenderer()); So, at each iteration I want to pass row and column in order to fill it out with the color. – Klausos Klausos Jan 30 '15 at 11:56
  • 1
    Post [MCVE](http://stackoverflow.com/help/mcve) for help. You can store some information for that inside your model. – alex2410 Jan 30 '15 at 11:59
  • 1
    `Imagine I am filling out the table inside the for loop:` This has nothing to do with rendering the data. The same renderer is used for every row in the column. So you renderer needs the custom logic to reset the color of the row every time the renderer is called. It is a dynamic process not a static one. – camickr Jan 30 '15 at 15:03