tags:

views:

219

answers:

2

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.

A: 

ActionMap and InputMap have getParent() and setParent(), so the solution is:

  1. Create a new map with the few changes you want to make.
  2. Query your component for it's map
  3. Set that map as the parent of your new map
  4. Install your new map in the component

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.

Aaron Digulla
this is not what i ask about
sereda
The same works with the InputMap, so i wonder why you can't simply extend the existing maps? Why do you want to copy all the keys to new maps?
Aaron Digulla
I have no problems extending input and action maps, and I don't want to copy all the keys. Sorry if this is not clear from my question's wording. My question is whether the keys for actions used in those maps are documented anywhere (in other words, how reliable is the code that assumes there's a key, say, "maxScroll" for the action that moves slider to the end of the scale)
sereda
Aaron, sorry I rated your answer -1 without realizing I probably didn't state the question clear enough. stackoverflow will allow me to undo the negative vote if you make any change to your answer
sereda
See my edits. As for the -1, just click the "down arrow" again to undo it.
Aaron Digulla
+2  A: 

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

    • MetalLookAndFeel
    • MotifLookAndFeel
    • WindowsLookAndFeel
  • MultiLookAndFeel

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";
Mario Ortegón