Each OS has a different scheme for changing language from keyboard or mouse. Is there any API in Java to support programmatically changing language?
What I would like is to have a translator with two JTextField objects. If I type in one, I am automatically typing in English, and if I type in the other, I would like to automatically switch to another language. If that language is Japanese or Chinese, I would like the OS to automatically switch to that language on entry to that component without having to manually switch each time. Here is some sample code using Locale and InputContext. It indicates that it succeeds in setting the context, but I type and English comes out. Contrast this with my switching to Japanese manually, where typing will result in japanese characters. What I am doing wrong?
public class TestLocale extends JFrame {
public TestLocale() {
super("TestLocale");
setSize(600,600);
JTextField a = new JTextField("English");
JTextField b = new JTextField("Japanese");
Locale loc =Locale.JAPANESE;
System.out.println("Script: " + loc.getScript());
System.out.println("Language: " + loc.getLanguage());
b.setLocale(loc);
b.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
InputContext c = InputContext.getInstance();
boolean b = c.selectInputMethod(Locale.JAPANESE);
System.out.println("Trying to request Japanese: " + b);
}
} );
add(BorderLayout.NORTH, a);
add(BorderLayout.SOUTH, b);
setVisible(true);
}
public static void main(String[] a) {
TestLocale t = new TestLocale();
}
}
Since Japanese is multibyte and more complex, I tried Greek. I can switch into greek right here: ασδκφξησκ (manually)
but when I select the Greek input context, nothing happens:
b.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
InputContext c = InputContext.getInstance();
boolean b = c.selectInputMethod(new Locale("el", "GR"));
System.out.println("Trying to request Greek: " + b);
}
} );