Name: rmT116609 Date: 09/13/2000
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)
======================================================================