开发者

Java Swing jtable cell editor doubles E numbers

开发者 https://www.devze.com 2022-12-26 00:49 出处:网络
Hi I an issue with editors in a JTable. I have a column which displays data as 26,687,489,800.00 ie: Double.

Hi I an issue with editors in a JTable.

I have a column which displays data as 26,687,489,800.00 ie: Double.

When the user clicks the cell to edi开发者_如何学编程t the data it is displayed as -2.66874908E10.

I want the data to be edited as it appears when it is displayed ie: 26,687,489,800.00 - without the E10 etc...

Any help would be appreciated.

Mike


You should employ a DecimalFormat instance to format your value correctly when you setup your editor.


The component used as editor is completely different from the one used to display data (the renderer). This is why you have difference of format between the two of them.

I recommend you to read this part of the Java tutorial, about adding your own cell editor. You should add a Formatted text field, to which you would put the number format you need.

Example:

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


If we consider that your column is of class Double you can do the following:

DecimalFormat df = new DecimalFormat ("#,##0.######");
JFormattedTextField fmtTxtField = new JFormattedTextField(df);
TableCellEditor cellEditor = new DefaultCellEditor(fmtTxtField);
table.setDefaultEditor(Double.class, new DefaultCellEditor(fmtTxtField));

but you need to overwrite the default delegate implementation of DefaultCellEditor. (At least in 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;
            }
        };
    }
}

And instead use:

table.setDefaultEditor(Double.class, new DoublesCellEditor(fmtTxtField));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号