while editing cell value in JTable it’s value suddenly appears in exponential form(i.e. 1.7E10) instead of being displayed in normal user friendly form.
How can I change the number formatting while editing cell value?
…
The Solution Was really simple, had to create my own TableCellEditor like that:
public static class Double2DecimalEditor extends AbstractCellEditor implements TableCellEditor{
private static final long serialVersionUID = 1L;
private JComponent component = new JTextField();
private NumberFormat nf;
@Override
public Object getCellEditorValue() {
System.out.println("getCellEditorValue");
NumberFormat nf = new DecimalFormat("#,###.###");
String text = ((JTextField)component).getText();
System.out.println("TEXT = " + ((JTextField)component).getText());
return nf.format(Double.parseDouble(text));
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
System.out.println("getCellEditorComponent");
Double data = Double.valueOf(value.toString().replace(",", ""));
NumberFormat nf = new DecimalFormat("#.###");
((JTextField)component).setText(nf.format(data));
((JTextField)component).setHorizontalAlignment(SwingConstants.RIGHT);
return component;
}
}
THANKS to everybody for advices!
As far as i see its this line that breaks your code
this happens in the javax.swing.DefaultCellEditor#DefaultCellEditor that is used by the JTable.
You will have to add your own editor that applies formatting .
should be similar too
applied to the example you change the makeTable with