Hi guys i have the following issue in my program. I have several tablecellrenderers which are applied by column. The problem is if a column appears two times in my table the cellrenderer won't work, and leaves both columns unformatted. I am also not able to change the column names because my customer has a specific idea of how this table should look like
I asked google, the stackoverflow search but i can't find any solution for this.
Do you know a workaround for this?
Thanks in advance
The Lines where i apply the cellrenderer
for(int i=0;i<(columnnames.length-anz_col);i++){
//Berechnung einmalig durchführen
actcol=i+anz_col;
//CellFormater
tbl_patchstand.getColumn(columnnames[actcol]).setCellRenderer(new PatchstandCellRenderer(tbl_patchstand.getDefaultRenderer(Object.class), actcol, maxfpatches[i]));
tbl_patchstand.getColumn(columnnames[actcol]).setCellRenderer(new PatchstandCellRenderer(tbl_patchstand.getDefaultRenderer(Double.class), actcol, maxfpatches[i]));
tbl_patchstand.getColumn(columnnames[actcol]).setCellRenderer(new PatchstandCellRenderer(tbl_patchstand.getDefaultRenderer(Integer.class), actcol, maxfpatches[i]));
}
The Cellrenderer itself:
public class PatchstandCellRenderer extends DefaultTableCellRenderer {
/**
*
*/
private int maxval,col;
private double oldval,newval;
public PatchstandCellRenderer(TableCellRenderer cellRenderer, int col, int maxval) {
super();
this.maxval=maxval;
this.col=col;
}
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);
if((column==col) && value!=null && (!((String)value).trim().equals("-")) && (!((String)value).trim().equals(""))){
//Umrechnen Wert auf Range
oldval=Double.parseDouble((String) value);
newval=(oldval)/(maxval);
c.setBackground(GetColor.getColor(newval));
}
else
c.setBackground(Color.WHITE);
return c;
}
}
The Cellrenderer gets a maxvalue of this column which is calculated before the cellrenderer will be applied. This maxvalue is used to get an color range from o to max in java hue color scheme.
Use column model to set a renderer for your column.
Instead of
tbl_patchstand.getColumn(columnnames[actcol])
use
tbl_patchstand.getColumnModel().getColumn(actcol)
Index must be unique for each column.
This should solve your problems.