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);
}
}
}
와
#,###,###,##0.00
을 변경합니다. 게시 해 주셔서 감사합니다! –