Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4370733
Votes 0
Synopsis AWTKeyStroke's getAWTKeyStroke(String) and toString() method aren't symmetric
Category java:classes_awt
Reported Against 1.3 , 1.3.1 , merlin-rc1 , merlin-beta2
Release Fixed 1.5(tiger)
State 10-Fix Delivered, request for enhancement
Priority: 4-Low
Related Bugs 4488881 , 4505112 , 4634696 , 4875345
Submit Date 13-SEP-2000
Description




java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

KeyStroke.getKeyStroke(String s) should accept the string returned by
KeyStroke.toString(). In particular, if there exists a <code>KeyStroke
a</code>, then <code>KeyStroke.getKeyStroke(a.toString)</code> should return.

Currently, KeyStroke.toString() does not return a string that matches the
documented grammar for KeyStroke.getKeyStroke(String). This makes the KeyStroke
class's toString method fairly useless.

In my particular project, I would like to be able to use the KeyStroke's
toString() method as a serialization mechanism in a properties file. Let's say
that I have an application that has user-defined accelerator keys (a.k.a.
hotkeys, shortcut keys). I have a Print bean in class org.brianlsmith.PrintBean
and I want to store the information for the PrintBean's shortcut key in a
property file (actually, a property-backed resource bundle). I need to have a
way to convert from a KeyStroke to a String and back, but there is no way to do
that short of writing my own parser for KeyStoke.toString()'s format (which is
unspecified) or by reimplementing KeyStroke.toString().

Since KeyStroke.toString()'s format is undocumented, there should be no
political problems in changing it to match KeyStroke.getKeyStroke(String)'s
documented grammer.

In the test program below, The output should be:
shift F1
shift F1
1st Copy equals original: true
shift F1
2nd Copy equals original: true

// BEGIN A.java
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;

public class A {
	public static void main(String [] args) {
		KeyStroke original = KeyStroke.getKeyStroke(KeyEvent.VK_F1,KeyEvent.SHIFT_MASK);
		System.out.println(original);

		KeyStroke firstCopy = KeyStroke.getKeyStroke(original.toString());
		System.out.println(firstCopy);
		System.out.println("1st Copy equals original: " + (original ==firstCopy));

		KeyStroke secondCopy = KeyStroke.getKeyStroke("shift F1");
		System.out.println(secondCopy);
		System.out.println("2nd Copy equals original: " + (original == secondCopy));
	}
}
// END A.java
(Review ID: 108792) 
======================================================================
Work Around




Overriding KeyStroke.getString() or providing a correctly-behaving KeyStroke-
>String converter is the best way to work around this issue.

Also look at using the numeric values for the key code and the shift mask
(although this would require some very simple parsing to split the two numbers
apart when they are written as a pair of strings).
======================================================================
Evaluation
KeyStroke is currently a swing class, so I'm assigning this rfe to them 
for evaluation.  
 xxxxx@xxxxx  2000-09-13

This is a reasonable suggestion, and we should consider it for a future release.
Any change should apply to both java.awt.AWTKeyStroke and javax.swing.KeyStroke.
 xxxxx@xxxxx  2001-07-13
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang