Say I have a standard Swing component like JSlider, but I want to slightly adjust the input map. Default input maps and action map are installed by look and feel, and I want to reuse some of the actions already available in ActionMap. To do that, I need to put ActionMap entry's key into the value of an InputMap's entry.
I can easily look up ActionMap keys (always a String) at runtime with debugger, and reuse it. It will work - guaranteed on my version of JDK and L&F.
So the question is, are the keys for default Swing component actions documented anywhere, can they "legally" change over time (that is, from JDK version to JDK version or from L&F to L&F) and have you seen such a change in practice?
Thanks.
Ok it took me a while to search this up.
In short, they don't seem to be standarized (much) and they don't see to be documented (much).
The class LookAndFeel is the key. This is the hierarchy:
LookAndFeel
BasicLookAndFeel
In the BasicLookAndFeel class, you can find the default mappings for actions and key bindings, which would be inherited by all other classes. So you could consider this class to be the standard. You can see that in the creation of the object "defaults", around like 498 for Java 1.4.2_17.
Additional key bindings and overwrites can be found on the implementors, such as WindowsLookAndFeel.
Some of the Standarized names can be found on the DefaultEditorKit class as static fields. Those seem to be safe to use and remap. Their usage can be seen in the WindowsLookAndFeel and MotifLookAndFeel classes. I would feel safe assuming that those actions will stay constant.
So in short, the Actions defined in DefaultEditorKit are unlikely to change. However, the key bindings change completely between L&F implementations. Retrieve the action from the map using DefaultEditorKit.something and it should work across versions. Example from DefaultEditorAction that you could possibly use with JSlider:
/**
* Name of the Action for extending the selection
* by moving the caret logically forward one position.
* @see #getActions
*/
public static final String selectionForwardAction = "selection-forward";
ActionMap
and InputMap
have getParent()
and setParent()
, so the solution is:
This way your modifications overwrite and extend the existing mappings.
[EDIT] I'm not aware that there is a list of all keys anywhere. But things like maxScroll
should be "stable", i.e. they should exist in future versions (not that Swing has changed much in the past 10 years ...)
So if you need to replace a certain mapping, use the approach above. This way, you keep all the existing mappings of the L&F (keeping the component usable even if you make a mistake). If you depend on overwriting a certain key, then I suggest to check whether the key exists and throw an error should it suddenly disappear.
This way, your code will work (probably for many years) and if it breaks, it will actively tell you about the change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With