Changing IME Language in java swing application

2019-08-12 14:04发布

问题:

I am building a Text Editor in Java(Swing) having an EditorPane to type the text and a Menu containing JRadioButtonMenuitems. Like Menu is "Language" and JRadiobuttonMenuitems under "Language" are "Spanish","Japanese","English" etc the scenario is that when user clicks and select any JRadioButtonMenuItem the system must change its IME as per the selected language. Like if user click and select Japanese option Editor must change the IME to Japanese for current process and allow user to enter text in respective language.

I've got the proper IME installed. Manually I can change the IME and able to write in a swing component. but my problem is how to load the IME programmatically.

Currently i am building this application on windowsXP but want this application to change IME language for each operating system.

I have google it but havenot found any related information to change IME.

Thanks

回答1:

IME Language can be changed for the JEditorPane by getting an InputContext instance and overriding getInputContext method for JEditorPane like.

final InputContext context = InputContext.getInstance();

jEditorPaneMain = new javax.swing.JEditorPane()
   {
       @Override
        public InputContext getInputContext() {
             return context;
        }
   };

and on selection of any language like on click of japanese JRadioButtonMenuItem add an ActionListener

jRadioButtonMenuItemJapanese.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItemJapaneseActionPerformed(evt);
            }
        }); 

do the following inside event handler.

private void jRadioButtonMenuItemJapaneseActionPerformed(java.awt.event.ActionEvent evt) {                                                            
      context.selectInputMethod(Locale.JAPANESE);

}

I have tried this out on Windows Xp. Its working perfectly fine.



标签: java swing io ime