-- I am trying to change the color of different cells in a JXTable based on their value. If a cell value is Negative it has to be Bold and Red, if not it will be simple Black.
The problem is, i am able to see the changed color of cells only when i select a Row. If i do not select a Row in the table all the cell values are of the same color that is
black. However, the font is bold for cells that should be displayed as red. But the color red is displayed only if i select the rows with negative cell value.
-- Can any one throw some light on this. I have tried my trial and errors. My concepts regarding cell rendered seem to be OK.
-- Please let me know if i am missing any thing or if you need any other information to help me out with this .
-- I am sorry i am pasting the well formatted code, but upon submiting the code become unformatted
JXTable(org.jdesktop.swingx.JXTable) named positionMonitorTable With the Table Properties set as -
-------------------------------------------------------------
positionMonitorTable.setColumnModel(PositionMonitor.getColumnModel()); - Table Model described below
positionMonitorTable.setDragEnabled(true);
positionMonitorTable.setDropMode(DropMode.INSERT_ROWS);
positionMonitorTable.setFillsViewportHeight(true);
positionMonitorTable.setBorder(BorderFactory.createRaisedBevelBorder());
positionMonitorTable.setColumnControlVisible(true);
positionMonitorTable.setHorizontalScrollEnabled(true);
positionMonitorTable.setAutoscrolls(true);
positionMonitorTable.setRowHeight(20);
positionMonitorTable.setHighlighters(HighlighterFactory.createSimpleStriping());
----------------------------------------------------------------------
The TableColumnModel as -
TableColumnModel model = new DefaultTableColumnModelExt();
TableColumn column = new TableColumnExt(VALUE);
column.setCellRenderer(new DecimalSupZFormatRendererTEST);
column.setHeaderValue(VALUE);
model.addColumn(column);
-----------------------------------------------------------------
Now the Detail of DecimalSupZFormatRendererTEST class-
public static class DecimalSupZFormatRendererTEST extends
DefaultTableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final NumberFormat formatter = NumberFormat
.getInstance(Locale.FRANCE);
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
JComponent comp = null;
if(value != null
&& (value instanceof Double || value instanceof Long)) {
Double val = Double.valueOf(1);
val = (Double) value;
value = formatter.format(value);
if(val.doubleValue() >= 0){
comp = (JComponent) super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
}else{
comp = (JComponent) super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
comp.setFont(new Font("Verdana", Font.BOLD, 11));
comp.setForeground(Color.RED);
}
}
setHorizontalAlignment(SwingConstants.RIGHT);
return comp;
}
}