Cannot change input language in jTextField

醉酒当歌 提交于 2019-12-12 04:54:00

问题


Why can't I switch input language in jtextfield and JOptionPane.showInputDialog()? On my computer I can, but on other computer I can write only system locale language symbols.

Ctrl+Shift or Ctrl+Alt+Shift is not working in application, but it is working when app is not focus

Locale.setDefault(Locale.ENGLISH); //tried it
System.setProperty("user.language", "en"); // and it

private void showPasswordWindow() {

    String pass = JOptionPane.showInputDialog(null, "Enter password", "Secure", JOptionPane.WARNING_MESSAGE);
    if (pass == null)
        System.exit(0);
    if (!pass.contains("somepassword"))
        showPasswordWindow();
}

Not working (( password contain English symbols and I can't to enter password (only Russia symbols working)

JRE 8; PS: I want to enter English symbols, but I can type only Russian symbols...NOT WORKING ONLY DIALOG TEXTFIELDS


回答1:


There are two issues here.

The first is entering the symbols. This isn't really a Java problem, so much as an issue with installing a different language for the keyboard.

The second issue is displaying foreign language symbols. You may need to use a UTF font to display characters that otherwise don't display right




回答2:


You should change the Locale:

textField.getInputContext().selectInputMethod(Locale.ENGLISH);

EDIT

Maybe it's not the best option, but you can try override the default DocumentFilter for the JtextField and change the encoding:

class EnglishTextField extends JTextField {

    public EnglishTextField() {
        ((AbstractDocument)getDocument()).setDocumentFilter(new DocumentFilter(){
            @Override
            public void insertString(DocumentFilter.FilterBypass fb, int offset,
                    String text, AttributeSet attr) throws BadLocationException {
                try {
                    //TODO change to your current encoding
                    byte[] bytes = text.getBytes("ISO-8859-1"); 
                    fb.insertString(offset, new String(bytes, "UTF-8"), attr);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}



回答3:


Finally...i did it O_O

frame.setVisible(true);

solved the problem, but i can't understand why??..

p.s. without the code on my comp app working nice



来源:https://stackoverflow.com/questions/26551513/cannot-change-input-language-in-jtextfield

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