I have to make one of my columns in JTable which is double type to get only numbers and to round all with precision 2. Not only to show them with this precision but instead to write the number into data with precision 2. In fact if I write 2.456 it should write 2.46 in the cell and in the data. How is the best way to do this ? With custom cell renderer or with custom cell editor ? I have this code but its dont change the data it only shows correct data on the cell
public class DoubleCellRenderer extends DefaultTableCellRenderer {
int precision = 0;
Number numberValue;
NumberFormat nf;
public DoubleCellRenderer(int aPrecision) {
super();
precision = aPrecision;
setHorizontalAlignment(SwingConstants.RIGHT);
nf = NumberFormat.getNumberInstance();
nf.setMinimumFractionDigits(aPrecision);
nf.setMaximumFractionDigits(aPrecision);
}
@Override
public void setValue(Object value) {
if ((value != null) && (value instanceof Number)) {
numberValue = (Number) value;
value = nf.format(numberValue.doubleValue());
}
super.setValue(value);
}
}