I have a drop-down box (Combo) which is used to select between several
different strings. For example (not the real one!):
Translate text into : English
French
Italian
Esperanto
....
Depending on which language is chosen, the Property will be set to the
string "English" or "French" etc.
All well and good, until one translates the text into other languages.
AFAICS one has to compare the response against the current list of
translations to find out which has been chosen. This is messy and seems
unnecessary.
Is it possible to define the property tag list as the fixed resource
keys (e.g. key_en, key_fr, key_it) and somehow translate to/from the
locale-specific text in the GUI?
So the property would only ever know about resource keys, but the
property editor would be able to work with the corresponding resource
values.
Seems to me this must be a common i18n problem, but I've been unable to
find anything in my searches.
Christian Schlichtherle - 14 Jun 2005 17:12 GMT
Hi,
one way to deal with this is to initialize the JComboBox with an array of
objects. The combobox will then display the return values of the toString()
method. This is where your internationalized string should go. If you change
the return values of your combo box later, you'll need to call repaint() on
the combo box in order to ensure the combo box recognizes the changes.
If you call getSelectedItem() on the combo box, you will always get the
array element as a reference, so you can use this to identify the user's
choice.
That's simple and worked fine for me.
However, if you have a combo box with hundreds or thousands of elements, you
should probably not call repaint() all the times. Consider implementing a
JComboBoxModel instead and notify the combo box by firing the corresponding
event (the combo box is a listener of the model) instead.
There is also a chapter on this in the online Swing tutorial at Sun's web
site, but I can't exactly recollect which chapter. Try the one on JComboBox.
Regards,
Christian