Changing IME Language in java swing application

萝らか妹 提交于 2019-12-24 06:44:51

问题


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.



来源:https://stackoverflow.com/questions/9903666/changing-ime-language-in-java-swing-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!