i have a JTable and a custom table model , wich has in some places Double.class
as column class. I read from a database and then insert result to the table. I want the numbers to be rendered with 2 decimals so i use this class
public class NumberCellRender extends DefaultTableCellRenderer {
DecimalFormat numberFormat = new DecimalFormat("#.00");
@Override
public Component getTableCellRendererComponent(JTable jTable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(jTable, value, isSelected, hasFocus, row, column);
if (c instanceof JLabel && value instanceof Number) {
JLabel label = (JLabel) c;
label.setHorizontalAlignment(JLabel.RIGHT);
Number num = (Number) value;
String text = numberFormat.format(num);
label.setText(text);
}
return c;
} }
The problem is when i call addRow, the Double.class columns didn't format
public void addRowData( IEntity entity ) {
getRowsData().add(entity);
fireTableDataChanged();
}
The question is when i call this i have to call something more to render the table??