views:

304

answers:

2

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 edit 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

+2  A: 

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

akf
Hi I have created a text field and have formatted it using:DecimalFormat df = new DecimalFormat ("#,###,###,###,###,###,##0.######");df.format (d);I have assigned the JTextField as the Editor for my column BUT the I still get the same problem?Any ideas? Am I missing somethingMike
Michael
+2  A: 

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); 
Gnoupi
Hi I have created a text field and have formatted it using:DecimalFormat df = new DecimalFormat ("#,###,###,###,###,###,##0.######");df.format (d);I have assigned the JTextField as the Editor for my column BUT the I still get the same problem?Any ideas? Am I missing somethingMike
Michael
@Michael - I think you should use a JFormattedTextField, for this task. I'm not sure what exactly you did with the regular JTextField and a DecimalFormat, but the normal use should be to create a JformattedTextField and give it the DecimalFormat as argument: http://java.sun.com/javase/7/docs/api/javax/swing/JFormattedTextField.html#JFormattedTextField(java.text.Format)
Gnoupi