【问题标题】:JTable cell editor number formatJTable 单元格编辑器编号格式
【发布时间】:2011-09-27 06:02:08
【问题描述】:

我需要在 jTable 中显示精确到小数点后 2 位的数字。为此,我创建了一个自定义单元格编辑器:

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

【问题讨论】:

  • 我已经对你的问题投了赞成票,但我希望我能再次这样做。感谢您发布!

标签: java swing jtable number-formatting tablecelleditor


【解决方案1】:

只需将#,###,###,##0.00 更改为#.###.###.##0,00

【讨论】:

    【解决方案2】:

    充分利用区域设置:

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

    【讨论】:

    • @StanislavL 您是否曾经需要为 JComponents 设置与默认返回所有今天的本地操作系统相同的区域设置?
    • 我已经根据上面的块更改了我的代码,但我仍然面临这个问题。双击时,编辑字段以区域设置格式正确显示值(它显示逗号作为德语区域设置的小数点),但它不接受以逗号作为小数点的编辑值(当我们只显示旧值时尝试将其编辑为包含逗号作为点的内容)
    • @Statue:请考虑创建一个SSCCE,这是一个不需要外部资源(例如图像或数据库)的小型可编译和可运行程序,它可以演示您的问题,然后将其作为编辑发布到你的问题。
    • 我已经通过覆盖 DefaultCellEditor 中的 getCellEditorValue 和 stopEditing 方法来使其完美运行。
    • @Statue:恭喜!!我很想看到您成功的单元格编辑器代码。可以发给我们吗?
    【解决方案3】:

    例如

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

    【讨论】:

      【解决方案4】:

      试试

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

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 2019-04-30
        • 1970-01-01
        • 2010-12-07
        • 1970-01-01
        • 2014-01-01
        • 2010-12-22
        • 2011-03-26
        • 1970-01-01
        相关资源
        最近更新 更多