Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6292739

Locale change at runtime doesn't affect text displayed for accelerator keys

    XMLWordPrintable

Details

    • x86
    • windows_xp

    Description

      FULL PRODUCT VERSION :
      java version "1.5.0_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      If you change the default locale at runtime, the text displayed for accelerator keys doesn't change, e.g. if you start with default locale Locale.GERMAN an accelerator for "control I", which shows up as "Strg-I", doesn't change to "Ctrl-I" when changing the Locale to Locale.US.

      This is due to the fact that the accelerator text is constructed by calls to java.awt.event.KeyEvent.getKeyModifiersText(int) and java.awt.event.KeyEvent.getKeyText(int) which in turn rely on java.awt.Toolkit.getProperty(String, String) for all Strings that need i18n. Toolkit.getProperty(String, String) returns a localized value from a *private* ResourceBundle that gets initialized for the current default locale by a static initializer and there is no method that ever changes this ResourceBundle again. So once this class has been loaded, the locale is fixed. This makes changing the locale for accelerators at runtime impossible.
      At least that is what I saw when looking into the source code.

      Related bug reports: 4790992, 4382830, 2037980

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Set default locale to Locale.GERMAN.
      Make sure that Toolkit has been loaded. (this imitates that you already have a GUI running and try to change the locale at runtime)
      Set default locale to Locale.US.
      Create a GUI with a menu item that has an accelerator for "control I".

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Accelerator should show up as "Ctrl-I".
      ACTUAL -
      Accelerator shows up as "Strg-I".

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;

      public class AcceleratorTest {

      public static void main(String[] args) {
      Locale.setDefault(Locale.GERMAN);

      // This should show that it is sufficient to have Toolkit loaded before
      // setting the locale to the desired value to not get the expected
      // result. This is what happens when changing the locale at runtime.
      // Comment the following line to see the expected result.
      System.out.println(Toolkit.getProperty("AWT.control", null));

      Locale.setDefault(Locale.US);

      JFrame frame = new JFrame("Accelerator Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(250, 250);
      JMenuBar menuBar = new JMenuBar();
      frame.setJMenuBar(menuBar);
      JMenu menu = new JMenu("Menu");
      menuBar.add(menu);

      JMenuItem menuItem = new JMenuItem("Item");
      menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
      InputEvent.CTRL_DOWN_MASK));
      menu.add(menuItem);

      frame.setVisible(true);
      }
      }
      ---------- END SOURCE ----------
      ###@###.### 2005-06-30 08:56:01 GMT

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jssunw Jitender S (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Imported:
                Indexed: