2011-07-02 152 views
6

我需要在jTable中顯示精確的2位小數。要做到這一點我創建了一個自定義單元格編輯器:用於英語語言環境JTable單元格編輯器編號格式

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; 
    } 
} 

此單元格編輯器可以完美運行,其中一個點作爲小數點。但在德語區域,它不接受以逗號作爲小數點的值。請讓我知道我的代碼中有什麼問題。提前致謝。

編輯: 這是我如何得到它的工作:

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); 
    } 
} 
} 
+1

我已經投了你的問題,但我希望我可以再次這樣做。感謝張貼! –

回答

7

使用語言環境,你的優勢:

//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))); 
+1

+1正是我也建議。 – StanislavL

+0

@StanislavL你有沒有一次需要設置JComponents相同區域設置的區域設置爲通過defalut返回所有今天的本機操作系統? – mKorbel

+0

我已經根據上述塊更改了我的代碼,但我仍然面臨問題。雙擊時,編輯字段在區域識別格式中顯示值正確(它顯示逗號作爲德語區域設置的小數點),但不接受以逗號作爲小數點的編輯值(當我們顯示舊值時嘗試將其編輯爲包含逗號作爲點的內容) –

2

例如

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); 
    } 
} 
0

嘗試

DecimalFormat numberFormat = new DecimalFormat("\\d*([\\.,\\,]\\d{0,2})?"); 
0

只是改變#,###,###,##0.00#.###.###.##0,00

相關問題