I need to make custom text field which will format numeric values according to local way of formatting. So I've made a clas:
public class NumberTextField extends JFormattedTextField
{...
constructor looks this like:
public NumberTextField()
{
formater=new NumberFormatter();
formater.setAllowsInvalid( false );
nf=NumberFormat.getInstance();
formater.setFormat( nf );
this.setFormatter( formater );
}
It ends up with:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at javax.swing.text.InternationalFormatter.valueToString(Unknown Source)
at javax.swing.JFormattedTextField$AbstractFormatter.install(Unknown Source)
at javax.swing.text.DefaultFormatter.install(Unknown Source)
at javax.swing.text.InternationalFormatter.install(Unknown Source)
at javax.swing.JFormattedTextField.setFormatter(Unknown Source)
at hr.ikb.nasa.gui.custom.NumberTextField.<init>(NumberTextField.java:65)
at hr.ikb.nasa.gui.custom.NumberTextField.main(NumberTextField.java:35)
Since google could not offer something usefull I wanted to see what can you say about it. Maybe it has no value to cope with - I tried to put in constructor this.setValue(new Double(0.0d));
or this.setText("0");
- it did not help..