Localized accelerators (JMenuItem hotkeys) in Swing

I am working on an English app on a German laptop on a Spanish OS.

Even though I explicitly set Locale.setDefault (Locale.ENGLISH) at the start of my application, I can see hotkexs in the menu as

CTRL + Mayรบsculas + C 

      

instead

CTRL + SHIFT + C 

      

which I passed to the KeyStroke object.

Not only is this word not localized in English as I pointed out, but also that it maps the SHIFT key to MAYUS (CAPS LOCK in English), so I think this is not only a language issue, but the keyboard map is good ...

So how can I overlay English for all the GUI components?

Thank!

+1


source to share


1 answer


You must make sure that you set the locale before any instrumentation code is executed. The following code shows the effect: if you move Locale.setDefault(Locale.GERMAN);

to any other line, it will display the default accelerator names again.

Instead of setting the locale inside your code, you can also add the following argument to the VM:

-Duser.language=DE

      



locale menu

public class MenuLocale {

    public static void main(String[] args) {
        Locale.setDefault(Locale.GERMAN);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                JMenuBar menubar = new JMenuBar();
                JMenu menu = new JMenu("Menu");
                JMenuItem menuitem = new JMenuItem("Menuitem");    
                menuitem.setAccelerator(KeyStroke.getKeyStroke('X', KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK));

                f.setJMenuBar(menubar);
                menubar.add(menu);
                menu.add(menuitem);

                f.pack();
                f.setVisible(true);
            }
        });
    }
}

      

+3


source







All Articles