Search code examples
javaswinglocalejoptionpanehebrew

Change input dialog locale to hebrew in java


My application is in Hebrew. The computers that are running my app all have English set as the default language and Hebrew as the secondary language

Every time they need to input stuff to the app, they have to "alt + shift" to change language.

In my last question --> Change input language in java I got a great idea of how to set the locale for text fields, and it worked perfectly !

Now I need to do the same in all my popup Input dialogues.

The previous solution was based on using the FocusGained method of the JTextField FocusListener, now I don't have a FocusGained option, at least as far as I know :)

IE:

response = JOptionPane.showInputDialog(requestLine.this, ("<html><b><font color=\"#8F0000  +
  +   \"size=\"10\" face=\"Ariel\">" + "הכנס סטטוס חדש: " + "</font></p></html>"), "");

This option pane asks for input, and stores it in a string, I need it to pop up ready for input in Hebrew .

Is that even possible ?

Thanks, Dave


Solution

  • This will duplicate showInputDialog with the locale change:

    public class LocaleOptionPane extends JFrame {
    
        public static void main(String[] args) {
    
            new LocaleOptionPane();
        }
    
        LocaleOptionPane() {
    
            Locale loc = new Locale("iw", "IL");
            String message = "<html><b><font color=\"#8F0000\" size=\"10\" face=\"Ariel\">" + "הכנס סטטוס חדש: " + "</font></p></html>";
    
            setVisible(true);
    
            JOptionPane pane = new JOptionPane(message, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
            pane.setWantsInput(true);
    
            JDialog dialog = pane.createDialog(this, UIManager.getString("OptionPane.inputDialogTitle", loc));
            dialog.getInputContext().selectInputMethod(loc); // pane.getInputContext... also works
            dialog.setVisible(true);
            dialog.dispose();
    
            String response = (String) pane.getInputValue();
            if (response == JOptionPane.UNINITIALIZED_VALUE)
                System.out.println("aborted");
            else
                System.out.println(response);
        }
    }
    

    Notes:

    • I didn't add the green question-mark icon because it didn't seem well placed anyway, though it can be added by changing JOptionPane.PLAIN_MESSAGE to JOptionPane.QUESTION_MESSAGE.
    • You can change the title through the pane.createDialog call.
    • Implement your own behavior when checking the value of response.
    • Change the parent component.