Java Swing jtable单元格编辑器使E数字加倍
您好,我在JTable中与编辑讨论了一个问题 我有一个列,显示数据为26687489800.00 ie:Double 当用户单击单元格编辑数据时,它显示为-2.66874908E10 我希望数据能够按照显示时的显示方式进行编辑,即:26687489800.00-不带E10等 任何帮助都将不胜感激Java Swing jtable单元格编辑器使E数字加倍,swing,editor,jtable,double,cell,Swing,Editor,Jtable,Double,Cell,您好,我在JTable中与编辑讨论了一个问题 我有一个列,显示数据为26687489800.00 ie:Double 当用户单击单元格编辑数据时,它显示为-2.66874908E10 我希望数据能够按照显示时的显示方式进行编辑,即:26687489800.00-不带E10等 任何帮助都将不胜感激 Mike在设置编辑器时,应该使用实例来正确格式化值 设置编辑器时,应使用实例正确格式化值 用作编辑器的组件与用于在渲染器中显示数据的组件完全不同。这就是为什么两者的格式不同 我建议您阅读这篇关于添加自己
Mike在设置编辑器时,应该使用实例来正确格式化值 设置编辑器时,应使用实例正确格式化值 用作编辑器的组件与用于在渲染器中显示数据的组件完全不同。这就是为什么两者的格式不同 我建议您阅读这篇关于添加自己的单元格编辑器的文章。您应该添加一个 例如:
DecimalFormat df = new DecimalFormat ("#,##0.######"); //you shouldn't need more "#" to the left
JFormattedTextField fmtTxtField = new JFormattedTextField(df);
TableCellEditor cellEditor = new DefaultCellEditor(fmtTxtField);
//This depends on how you manage your cell editors. This is for the whole table, not column specific
table.setCellEditor(cellEditor);
用作编辑器的组件与用于在渲染器中显示数据的组件完全不同。这就是为什么两者的格式不同 我建议您阅读这篇关于添加自己的单元格编辑器的文章。您应该添加一个 例如:
DecimalFormat df = new DecimalFormat ("#,##0.######"); //you shouldn't need more "#" to the left
JFormattedTextField fmtTxtField = new JFormattedTextField(df);
TableCellEditor cellEditor = new DefaultCellEditor(fmtTxtField);
//This depends on how you manage your cell editors. This is for the whole table, not column specific
table.setCellEditor(cellEditor);
如果我们认为你的专栏是双层的,你可以做如下:
DecimalFormat df = new DecimalFormat ("#,##0.######");
JFormattedTextField fmtTxtField = new JFormattedTextField(df);
TableCellEditor cellEditor = new DefaultCellEditor(fmtTxtField);
table.setDefaultEditor(Double.class, new DefaultCellEditor(fmtTxtField));
但是您需要覆盖DefaultCellEditor的默认委托实现。至少在Java6中是这样
//DEFAULT IMPLEMENTATION INSIDE THE CONSTRUCTOR
....
public DefaultCellEditor(final JTextField textField) {
editorComponent = textField;
this.clickCountToStart = 2;
delegate = new EditorDelegate() {
public void setValue(Object value) {
textField.setText((value != null) ? value.toString() : "");
}
public Object getCellEditorValue() {
return textField.getText();
}
};
textField.addActionListener(delegate);
}
....
//YOUR IMPLEMENTATION
public class DoublesCellEditor extends DefaultCellEditor {
private JFormattedTextField textField;
public DoublesCellEditor(JFormattedTextField jft) {
super(jft);
this.textField = jft;
super.delegate = new EditorDelegate() {
public void setValue(Object value) {
textField.setValue(value != null ? ((Number) value).doubleValue() : value);
}
public Object getCellEditorValue() {
Object value = textField.getValue();
return value != null ? ((Number) value).doubleValue() : value;
}
};
}
}
而是使用:
table.setDefaultEditor(Double.class, new DoublesCellEditor(fmtTxtField));
如果我们认为你的专栏是双层的,你可以做如下:
DecimalFormat df = new DecimalFormat ("#,##0.######");
JFormattedTextField fmtTxtField = new JFormattedTextField(df);
TableCellEditor cellEditor = new DefaultCellEditor(fmtTxtField);
table.setDefaultEditor(Double.class, new DefaultCellEditor(fmtTxtField));
但是您需要覆盖DefaultCellEditor的默认委托实现。至少在Java6中是这样
//DEFAULT IMPLEMENTATION INSIDE THE CONSTRUCTOR
....
public DefaultCellEditor(final JTextField textField) {
editorComponent = textField;
this.clickCountToStart = 2;
delegate = new EditorDelegate() {
public void setValue(Object value) {
textField.setText((value != null) ? value.toString() : "");
}
public Object getCellEditorValue() {
return textField.getText();
}
};
textField.addActionListener(delegate);
}
....
//YOUR IMPLEMENTATION
public class DoublesCellEditor extends DefaultCellEditor {
private JFormattedTextField textField;
public DoublesCellEditor(JFormattedTextField jft) {
super(jft);
this.textField = jft;
super.delegate = new EditorDelegate() {
public void setValue(Object value) {
textField.setValue(value != null ? ((Number) value).doubleValue() : value);
}
public Object getCellEditorValue() {
Object value = textField.getValue();
return value != null ? ((Number) value).doubleValue() : value;
}
};
}
}
而是使用:
table.setDefaultEditor(Double.class, new DoublesCellEditor(fmtTxtField));
您好,我已经创建了一个文本字段,并使用以下格式对其进行了格式化:DecimalFormat df=new DecimalFormat,,,,,0。;df.d格式;我已经指定JTextField作为我专栏的编辑器,但是我仍然遇到同样的问题?有什么想法吗?我错过什么了吗Mike@Michael-我认为您应该使用JFormattedTextField来完成此任务。我不确定您对常规JTextField和DecimalFormat到底做了什么,但通常的用法应该是创建一个JformattedTextField并将DecimalFormat作为参数:您好,我创建了一个文本字段,并使用DecimalFormat df=new DecimalFormat,,,,,,0对其进行了格式化。;df.d格式;我已经指定JTextField作为我专栏的编辑器,但是我仍然遇到同样的问题?有什么想法吗?我错过什么了吗Mike@Michael-我认为您应该使用JFormattedTextField来完成此任务。我不确定您对常规JTextField和DecimalFormat到底做了什么,但通常的用法应该是创建一个JformattedTextField并将DecimalFormat作为参数:您好,我创建了一个文本字段,并使用DecimalFormat df=new DecimalFormat,,,,,,0对其进行了格式化。;df.d格式;我已经指定JTextField作为我专栏的编辑器,但是我仍然遇到同样的问题?有什么想法吗?MikeHi我创建了一个文本字段,并使用:DecimalFormat df=new DecimalFormat,,,,,0对其进行了格式化。;df.d格式;我已经指定JTextField作为我专栏的编辑器,但是我仍然遇到同样的问题?有什么想法吗?我错过什么了吗,迈克