Saving keyboard layout in JTextfield?

A simple example: 2 JTextFields, one for the Spanish word, the other for translation. Is there a way to keep the keyboard layout on JTextFieldso that the user does not have to switch back and forth?

TIA.

+3
source share
3 answers

Yes, this demo code uses a keyboard layout for the selected locales in each text box:

public class InputMethodTest {

  public static void main(String[] args) {
    final InputContext en = InputContext.getInstance();
    en.selectInputMethod(Locale.UK);
    final InputContext es = InputContext.getInstance();
    es.selectInputMethod(new Locale("es", "ES"));
    JTextArea english = new JTextArea() {
      @Override
      public InputContext getInputContext() {
        return en;
      }
    };
    JTextArea spanish = new JTextArea() {
      @Override
      public InputContext getInputContext() {
        return es;
      }
    };

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout());
    frame.getContentPane().add(new JScrollPane(english));
    frame.getContentPane().add(new JScrollPane(spanish));
    frame.setSize(600, 400);
    frame.setVisible(true);
  }
}

Tested on Windows XP Home with EN and ES keyboard layouts installed (via Control Panel & "Regional and Language Settings> Languages> Details ...). For more details, see Java Method Method Framework .

+5
source

, .

0

If you know exactly the layout of the Spanish keyboard, you can theoretically process KeyEvents yourself by translating them into the corresponding character. However, this would not be easy to do. You will probably end up inserting characters into text fields yourself.

0
source

Source: https://habr.com/ru/post/1707861/


All Articles