1

Possible Duplicate:
JSpinner editor locale

I have to show 1000 value as 1.000 in the spinner in java , by default it is showing as 1,000. But in European locale the number should be in 1.000,00 format instead of 1,000.00 . Can someone help me with this. I am using below code:

 JSpinner.NumberEditor editor = new JSpinner.NumberEditor(spinner, pattern);

 spinner.setEditor(editor);

and using pattern as "##.##0", but this is throwing exception. Can I write generic code for this which can take care of number format according to locale ie., en, fr, pt etc.?

Community
  • 1
  • 1
nishantsatyam
  • 27
  • 1
  • 2
  • 8
  • There is not such thing like European locale. Each country in Europe is a individual deal so the do not share locale settings. ;-) – Damian Leszczyński - Vash Oct 19 '12 at 07:53
  • _by default it is showing as 1,000_ hmm ... normally, the format is localized implying that either the default locale on your machine _is_ a locale using the comma or your machine locale is not supported by the jdk. So the question is: What is your locale? – kleopatra Oct 19 '12 at 08:45
  • I am fetching data from the server, and server locale is french. – nishantsatyam Oct 19 '12 at 08:50
  • simply, what I need to do to get 1,000 as 1.000 in the spinner if we just forget locales for a moment. – nishantsatyam Oct 19 '12 at 09:02
  • you can't forget locales, that's the whole point :-) You want to format the values as appropriate to _a_ locale. So first you have to decide _which_ locale the ui should show up. Typically, that's the default of the client machine. If you want to show it in a different locale, you have to use a format/ter in that different locale. – kleopatra Oct 19 '12 at 09:26

1 Answers1

2

Try creating or get the locale explicitly:

JSpinner spinner = new JSpinner();
String pattern = "0.000"; // valid format
JSpinner.NumberEditor editor = new JSpinner.NumberEditor(spinner, pattern);
DecimalFormat format = editor.getFormat();
Locale locale = new Locale("ru", "RU"); // create your specific locale
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
format.setDecimalFormatSymbols(decimalFormatSymbols);
spinner.setEditor(editor);
kapandron
  • 3,546
  • 2
  • 25
  • 38
  • the above code showing 1000 as 1000.000 in the spinner, I want 1000 as 1.000 , please provide the format to be used. – nishantsatyam Oct 19 '12 at 08:47
  • I guess it depends on the locale. Try define the format pattern so `pattern = "0,000";`. I hope you catch my idea – kapandron Oct 19 '12 at 09:06