2

I've to color text in cells of one column if value in it is greater than zero. I known, that there was already asks like this, but i can't find working solution despite several hours of searching. Everything gives me some errors. Solution, that gives me least errors is this:

public class MyRenderer extends DefaultTableCellRenderer  
{ 

    public Component getTableCellRendererComponent(JTable table, Object value, boolean   isSelected, boolean hasFocus, int row, int column) 
{ 
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
    c.setForeground(Color.RED);
    double values = Double.parseDouble(c.getInputContext().toString());
    if(column == 2){
        if(values > 0){
            c.setForeground(Color.GREEN);
        }
    }
    return c; 
} 

}

however when i've get references to this like that:

MyRenderer.getTableCellRendererComponent(table, "ok", true, true, 2, 2);

It gives me this error:

Cannot make a static reference to the non-static method getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int) from the type bitc.MyRenderer

But when the method is static, the method gives me this error...:

This static method cannot hide the instance method from DefaultTableCellRenderer

And i've got no idea how to bypass this.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
anat
  • 53
  • 6
  • for better help sooner post an SSCCE / MCVE, short, runnable, compilable, with hardcoded value for JTable/XxxTbaleModel in local variable – mKorbel Jul 30 '15 at 18:13

1 Answers1

1

I wonder when you try to call MyRenderer.getTableCellRendererComponent(table, "ok", true, true, 2, 2);?

Usually should do something like this:

table.setDefaultRenderer(Double.class, new MyRenderer());

or

table.getColumnModel().getColumn(columnIndex).setCellRenderer(new MyRenderer());
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • Now i've got this error: `No enclosing instance of type bitc is accessible. Must qualify the allocation with an enclosing instance of type bitc (e.g. x.new A() where x is an instance of bitc).` (my English is not perfect, so sorry for mistakes and i might misunderstand something) – anat Jul 30 '15 at 17:43
  • But you understand that it is complaining about `bitc`? And how should I know what `bitc` is in your context, without a [MCVE](http://stackoverflow.com/help/mcve)? Just a guess: MyRenderer is an inner class and you are using it in a static context? In that case you can make it a `static` class. – Tobias Liefke Jul 30 '15 at 18:44