Yan Pak
Yan Pak

Reputation: 1867

Why JVM doesn't pickup input text locale on Linux

I'm trying to get locale in which user inputs text into JPasswordField.

For this I'm doing the next:

final JPasswordField passwdField = new JpasswordField();
final InputContext it = InputContext.getInstance();
final JTextArea localeLng = new JTextArea();
...
...(some code) 
... 
passwdField.addKeyListener(new KeyAdapter() {
  @Override
  public void keyReleased(KeyEvent e) {
       localeLng.setText(it.getLocale().getLanguage().toUpperCase());
   }
});

I have two keyboard layouts En and Ru. When I switch between them it doesn't affect on the localeLng value. This perhaps on Ubuntu with JRE 7up7.
But this perfectly works on Windows 7.

So, in what may resides my problem at all?


My temporal rough solution :)

public void keyReleased(KeyEvent e) {
    int key = (int)e.getKeyChar();
    if(key>122){
        localeLng.setText("!");
        localeLng.setBackground(Color.RED);
    } else {
        localeLng.setText("En");
        localeLng.setBackground(Color.BLUE);
    }
}

Upvotes: 0

Views: 199

Answers (1)

user1797612
user1797612

Reputation: 832

It's probably a good thing to remind that with Java you get a well defined set of methods and classes but different implementations, there are many JVM out there and many OS that offers support for Java, probably each one with its own implementation.

That said, this link will answer your question.

I also suggest to let the user decide what is the right locale, getting the right locale programmatically can be really tricky, especially under Linux.

Upvotes: 1

Related Questions