2011-07-02 168 views
6

Tôi cần hiển thị số trong jTable với chính xác 2 chữ số thập phân. Để thực hiện việc này, tôi đã tạo trình chỉnh sửa ô tùy chỉnh là:Định dạng số biên tập viên 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; 
    } 
} 

Trình chỉnh sửa ô này hoạt động hoàn hảo cho ngôn ngữ tiếng Anh, nơi dấu chấm được sử dụng làm dấu thập phân. Nhưng trong ngôn ngữ của Đức, nó không chấp nhận các giá trị bằng dấu phẩy dưới dạng dấu thập phân. Vui lòng cho tôi biết nơi có sự cố trong mã của tôi. Cảm ơn trước.

EDIT: Dưới đây là làm thế nào tôi nhận nó làm việc:

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

Tôi đã bỏ phiếu bình chọn cho câu hỏi của bạn, nhưng tôi ước tôi có thể làm lại như vậy. Cảm ơn bạn đã đăng bài đó! –

Trả lời

7

Sử dụng bản địa để lợi thế của bạn:

//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 chính xác những gì tôi cũng sẽ đề xuất. – StanislavL

+0

@StanislavL bạn đã từng thời gian cần thiết lập Locale cho JComponents cùng Locale như trả lại tất cả các hệ điều hành gốc ngày hôm nay bằng cách định giá? – mKorbel

+0

Tôi đã thay đổi mã của mình theo khối trên nhưng tôi vẫn đang gặp sự cố. Khi nhấp đúp, trường chỉnh sửa hiển thị giá trị đúng theo định dạng nhận dạng ngôn ngữ (nó hiển thị dấu phẩy là dấu thập phân cho ngôn ngữ Đức), nhưng không chấp nhận giá trị được chỉnh sửa bằng dấu phẩy dưới dạng dấu thập phân (khi chỉ hiển thị giá trị cũ khi chúng tôi cố gắng sửa nó thành một cái gì đó có chứa dấu phẩy như điểm) –

2

ví dụ

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

Hãy thử

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

Chỉ cần thay đổi #,###,###,##0.00 với #.###.###.##0,00

Các vấn đề liên quan