0

I'm trying to change the colour of one or more particular cells in a column in my JTable. The answers I have seen on here all refer to this particular method;

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    Component y = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    y.setBackground(new Color(255,0,0));

    return y;
}

But the problem is I do not understand in any way how this works, in my other class I have a Jtable of strings and I want to change the colour of certain cells according to their string value, however the solutions i find only allow me to change the colour of an entire column of cells in the jtable and not a specific one.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Your question appears to be perhaps a little premature. You need to learn how to use a TableCellRenderer such as by extending either DefaultTableCellRenderer or the AbstractTableCellRenderer. If you read the [JTable tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html), the respective API's, and search this site a little more diligently, you'll find all the information that you'll need to at least make an initial stab at this. – Hovercraft Full Of Eels Feb 10 '14 at 21:05

2 Answers2

5

I have a Jtable of strings and I want to change the colour of certain cells according to their string value

The same renderer is used for all cells so you need to reset the background every time.

You will need an if condition in your custom renderer code. Something like:

if (!isSelected)
    if (value.equals(...))
        y.setBackground(new Color(255,0,0));
    else
        y.setBackground(table.getBackground())
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    well, nitpicking (and not really helping the OP, as s/he appears to need taking a step back and start with something more basic ;-): checking only selected state isn't good enough, there might be more states to differentiate (like dropLocation, focused && editable, ...). [A more complete solution](http://stackoverflow.com/a/9617446/203657) - biased me - is to check for one's own condition _before_ calling super and if not interested, set the color to null – kleopatra Feb 10 '14 at 23:11
0

You can use DefaultTableCellRenderer to color alternate row from JTable.

table.setDefaultRenderer(Object.class, new TableCellRenderer(){
    private DefaultTableCellRenderer DEFAULT_RENDERER =  new DefaultTableCellRenderer();

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if(isSelected){
                    c.setBackground(Color.YELLOW);
                }else{
                if (row%2 == 0){
                    c.setBackground(Color.WHITE);

                }
                else {
                    c.setBackground(Color.LIGHT_GRAY);
                }     }

       //Add below code here
                return c;
            }

        });

If you want to color your row using the value of a particular row then you can use something like this. Add these line to above

if(table.getColumnModel().getColumn(column).getIdentifier().equals("Status")){//Here `Status` is column name
    if(value.toString().equals("OK")){//Here `OK` is the value of row

        c.setBackground(Color.GREEN);
    }   
}
ravibagul91
  • 20,072
  • 5
  • 36
  • 59