JDK-6292739 : Locale change at runtime doesn't affect text displayed for accelerator keys
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6.0,5.0,7,8,9
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-06-30
  • Updated: 2019-03-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Relates :  
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

Comments
related mail thread : http://mail.openjdk.java.net/pipermail/awt-dev/2015-October/010258.html
02-11-2015

EVALUATION Description and analysis seems reasonable. Will investigate.
03-08-2005