I want to create a simple test application with a JEditorPane displaying some HTML content and a button which makes the selected text bold. The HTMLEditorKit already provides the necessary action for this button so I was able to use it with complicated code like this:
// Build action map
Map<String, Action> actionMap = new HashMap<String, Action>();
for (Action action: editor.getActions())
actionMap.put("" + action.getValue(Action.NAME), action);
// Get the action
Action action = actionMap.get("font-bold");
But I'm pretty sure there is something wrong. I can't believe that I have to create this helper map to get the action by name. The only official method I found for retrieving the actions seems to be this getActions() method of JEditorPane which just returns an array.
So is there a better way to fetch a specific action from an EditorKit
and I simply missed it or is it just bad API design?
Why do you fill your own map?
editor.getActionMap().allKeys()
editor.getActionMap().get("font-bold")