gpt4 book ai didi

java - JTable 单元格编辑器数字格式

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:18:50 28 4
gpt4 key购买 nike

我需要在 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);
}
}
}

最佳答案

充分利用语言环境:

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

关于java - JTable 单元格编辑器数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6556651/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com