Are default action names standardized in the Swing component of the ActionMap component?

Say I have a standard Swing component like JSlider, but I want to tweak the input map slightly. By default, I / O cards and an action card are installed using the appearance and I want to reuse some of the actions already available in ActionMap. To do this, I need to enter the ActionMap activation key in the value of the InputMap entry.

I can easily find ActionMap keys (always String) at runtime using the debugger and reuse it. This will work - guaranteed in my version of JDK and L & F.

So, the question is whether the keys for Swing component actions are documented by default somewhere, can they "legally" change over time (that is, from the JDK version to the JDK version or from L & F to L & F) and Have you seen such a change in practice?

Thanks.

+6
java swing
source share
2 answers

Well, it took me a while to find this.

In short, they don't seem to be standardized (a lot), and they don't see documented (a lot).

The LookAndFeel class is the key. This is the hierarchy:

  • LookAndFeel

  • BasicLookAndFeel

    • MetalLookAndFeel
    • MotifLookAndFeel
    • WindowsLookAndFeel
  • MultiLookAndFeel

In the BasicLookAndFeel class, you can find default mappings for actions and key bindings that will be inherited by all other classes. So you can consider this class standard. You can see that when you create the object "by default", approximately like 498 for Java 1.4.2_17.

Additional bindings and key rewriting can be found at developers such as WindowsLookAndFeel.

Some of the Standarized names can be found in the DefaultEditorKit class as static fields. They seem to be safe to use and reassign. Their use can be seen in the WindowsLookAndFeel and MotifLookAndFeel classes. I would be sure that these actions would remain constant.

In short, the actions defined in DefaultEditorKit are unlikely to change. However, the key bindings completely change between L & F implementations. Extract the action from the map using DefaultEditorKit.something, and it should work in different versions. An example from DefaultEditorAction that you could 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"; 
+4
source share

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

  • Create a new map with the small changes you want to make.
  • Request your component for it map
  • Set this card as the parent of your new card.
  • Install a new card in the component

This way, your modifications overwrite and extend existing mappings.

[EDIT] I do not know that there is a list of all keys anywhere. But things like maxScroll should be "stable", i.e. Must exist in future versions (not that Swing has changed much in the last 10 years ...)

So, if you need to replace a specific mapping, use the approach described above. This way you keep all existing L & F mappings (while maintaining the usefulness of the component, even if you make a mistake). If you depend on overwriting a particular key, I suggest checking if the key exists and throwing an error if it suddenly disappears.

This way your code will work (possibly for many years), and if it breaks, it will actively tell you about the changes.

+3
source share

All Articles