Changing the background of a JTable cell

Could anyone help me with this? Ive read everything I could find on t his subject, I understand you have to use a custom cellrenderer but couldnt get it to work.

What am I doing wrong?

publicclass CellRendererextends DefaultTableCellRenderer

{

public CellRenderer(){

setOpaque(true);//MUST do this for background to show up.

}

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

Component renderer = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);

renderer.setBackground(Color.RED);

System.out.println("FFS WORK FUCKING RENDERER!");

setOpaque(true);

return renderer;

}

}

//tabel is a JTable

TableColumnModel colModel = tabel.getColumnModel();

TableColumn t = tabel.getColumnModel().getColumn(0);

TableColumn d = tabel.getColumnModel().getColumn(1);

CellRenderer a =new CellRenderer();

t.setCellRenderer(a);

d.setCellRenderer(a);

[1773 byte] By [Blueboboa] at [2007-11-27 7:36:20]
# 1
Try setting the renderer opaque, instead of whatever it is you are actually setting opaque in that code.
DrClapa at 2007-7-12 19:16:51 > top of Java-index,Java Essentials,Java Programming...
# 2

In the future, Swing related questions should be posted in the Swing forum.

By default the DefaultTableCellRenderer is opaque so you don't need to worry about that.

> but couldnt get it to work.

You don't say whether you get the System.out.println(...) or not so we don't know if the problem is with the renderer or with adding the renderer to the column.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 19:16:51 > top of Java-index,Java Essentials,Java Programming...