Locale locales[]=Locale.getAvailableLocales();
A word of caution is that not all listed locales are available for InputContext -- of those, only that has both the language code and the country code can be set to use. But once you have the array of strings for the available locales, this input dialog will do the job nicely to put all locales in scrollable pane and wait for user input. Here, names is the string array that contains the list of locales.
Object selectedValue = JOptionPane.showInputDialog(null,
"Select a locale", "Language",
JOptionPane.INFORMATION_MESSAGE, null,
names,names[0]);
Perhaps it gets more clearer if you look at the code so I put it right here. Here all the locales that only have either the country code or the language code are filtered out. This is what this following code does: the list of locales will be presented and then when a value is selected, the locale will be set for the InputContext.
As of 2008-1-16, the code has been modified so that the list will be sorted out in alphabetical order.
public void langList(JMenuItem mi)
{
int x=mi.getX();
x+=mi.getWidth();
int y=mi.getY();
Locale locales[]=Locale.getAvailableLocales();
String names[]=new String[locales.length];
String languages[]=new String[locales.length];
String countries[]=new String[locales.length];
int index=0;
for(int i=0;i<locales.length;i++)
{
languages[index]=locales[i].getLanguage();
countries[index]=locales[i].getCountry();
if(!languages[index].isEmpty() && !countries[index].isEmpty())
{
names[index]=locales[i].getDisplayName();
index++;
}
}
int nLocales=index;
int sortedIndex[]=new int[nLocales];
String sortedNames[]=new String[nLocales];
for(int i=0;i<nLocales;i++)
sortedIndex[i]=i;
for(int i=0;i<nLocales;i++)
{
for(int j=i+1;j<nLocales;j++)
{
if(0<names[sortedIndex[i]].compareTo(names[sortedIndex[j]]))
{
int t=sortedIndex[i];
sortedIndex[i]=sortedIndex[j];
sortedIndex[j]=t;
}
}
}
for(int i=0;i<nLocales;i++)
{
sortedNames[i]=names[sortedIndex[i]];
}
if(0<nLocales)
{
Object selectedValue = JOptionPane.showInputDialog(null,
"Set Language Environment", "Language",
JOptionPane.INFORMATION_MESSAGE, null,
sortedNames,sortedNames[0]);
int i=0;
if(selectedValue!=null)
{
while(!((String)selectedValue).equals(names[i]) && ++i<nLocales);
if(i<nLocales)
{
Locale newLocale=new Locale(languages[i],countries[i]);
inputContext.selectInputMethod(newLocale);
}
}
}
}