7

I need to show numbers in jTable with exact 2 decimal places. To accomplish this I have created a custom cell editor as:

public class NumberCellEditor extends DefaultCellEditor {
    public NumberCellEditor(){
        super(new JFormattedTextField());
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        JFormattedTextField editor = (JFormattedTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);

        if (value!=null){
            DecimalFormat numberFormat = new DecimalFormat("#,##0.00;(#,##0.00)");
            editor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(numberFormat)));
            Number num = (Number) value;  
            String text = numberFormat.format(num);
            editor.setHorizontalAlignment(SwingConstants.RIGHT);
            editor.setText(text);
        }
        return editor;
    }
}

This cell editor works perfect for English locale where a dot is used as decimal point. But in German locale, it doesn't accept values with comma as a decimal point. Please let me know where there is a problem in my code. Thanks in advance.

EDIT: Here is how I got it working:

public class NumberCellEditor extends DefaultCellEditor {
public NumberCellEditor(){
    super(new JFormattedTextField());
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    JFormattedTextField editor = (JFormattedTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);

    if (value instanceof Number){
        Locale myLocale = Locale.getDefault(); 

        NumberFormat numberFormatB = NumberFormat.getInstance(myLocale);
        numberFormatB.setMaximumFractionDigits(2);
        numberFormatB.setMinimumFractionDigits(2);
        numberFormatB.setMinimumIntegerDigits(1);

        editor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                        new NumberFormatter(numberFormatB)));

        editor.setHorizontalAlignment(SwingConstants.RIGHT);
        editor.setValue(value);
    }
    return editor;
}

@Override
public boolean stopCellEditing() {
    try {
        // try to get the value
        this.getCellEditorValue();
        return super.stopCellEditing();
    } catch (Exception ex) {
        return false;
    }

}

@Override
public Object getCellEditorValue() {
    // get content of textField
    String str = (String) super.getCellEditorValue();
    if (str == null) {
        return null;
    }

    if (str.length() == 0) {
        return null;
    }

    // try to parse a number
    try {
        ParsePosition pos = new ParsePosition(0);
        Number n = NumberFormat.getInstance().parse(str, pos);
        if (pos.getIndex() != str.length()) {
            throw new ParseException(
                    "parsing incomplete", pos.getIndex());
        }

        // return an instance of column class
        return new Float(n.floatValue());

    } catch (ParseException pex) {
        throw new RuntimeException(pex);
    }
}
}
Khalid Amin
  • 872
  • 3
  • 12
  • 26

4 Answers4

8

Use the locale to your advantage:

  //Locale myLocale = Locale.GERMANY;  //... or better, the current Locale

  Locale myLocale = Locale.getDefault(); // better still

  NumberFormat numberFormatB = NumberFormat.getInstance(myLocale);
  numberFormatB.setMaximumFractionDigits(2);
  numberFormatB.setMinimumFractionDigits(2);
  numberFormatB.setMinimumIntegerDigits(1);

  edit.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                    new NumberFormatter(numberFormatB)));
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @StanislavL did you once time need to set Locale for JComponents same Locale as return all todays native OS by defalut? – mKorbel Jul 02 '11 at 18:38
  • I have changed my code according to the above block, but I am still facing the problem. When double-clicked, the edit field shows value properly in locale aware format (it shows comma as decimal point for German locale), but it does not accept an edited value with comma as a decimal point (when just shows the old value when we try to edit it to something that contains comma as point) – Khalid Amin Jul 03 '11 at 16:24
  • 2
    @Statue: please consider creating an [SSCCE](http://sscce.org), a small compilable and runnable program that requires no outside resources (for example images or database) and which demonstrates your problem and then post it as an edit to your question. – Hovercraft Full Of Eels Jul 03 '11 at 17:16
  • I have managed to get it working perfectly by overriding getCellEditorValue and stopEditing methods from DefaultCellEditor. – Khalid Amin Jul 03 '11 at 19:05
  • @Statue: Congrats!! I'd love to see your successful cell editor code. Can you post it for us? – Hovercraft Full Of Eels Jul 03 '11 at 19:33
  • Sure, should I post the code here in comments? Or is there a more appropriate place to post it? – Khalid Amin Jul 04 '11 at 04:24
  • @Statue: you can't post in comments. Instead post it as an edit to your original post. – Hovercraft Full Of Eels Jul 04 '11 at 10:27
2

for example

import java.text.NumberFormat;
import java.math.RoundingMode;
import javax.swing.table.DefaultTableCellRenderer;


public class SubstDouble2DecimalRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;
    private int precision = 0;
    private Number numberValue;
    private NumberFormat nf;

    public SubstDouble2DecimalRenderer(int p_precision) {
        super();
        setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        precision = p_precision;
        nf = NumberFormat.getNumberInstance();
        nf.setMinimumFractionDigits(p_precision);  
        nf.setMaximumFractionDigits(p_precision);
        nf.setRoundingMode(RoundingMode.HALF_UP);  
    }

    public SubstDouble2DecimalRenderer() {
        super();
        setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        nf = NumberFormat.getNumberInstance();
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        nf.setRoundingMode(RoundingMode.HALF_UP);
    }

    @Override
    public void setValue(Object value) {
        if ((value != null) && (value instanceof Number)) {
            numberValue = (Number) value;
            value = nf.format(numberValue.doubleValue());
        }
        super.setValue(value);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Just change #,###,###,##0.00 with #.###.###.##0,00

Saturnix
  • 10,130
  • 17
  • 64
  • 120
0

Try

DecimalFormat numberFormat = new DecimalFormat("\\d*([\\.,\\,]\\d{0,2})?");
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417