2

I want to change the ActionMap of Swing JTextFields in my entire Application by replacing a few actions by my custom implmentations. For an explanation why you can refer to the following post:

How do I make the caret of a JTextComponent skip selected text?

This works well if I do this:

ActionMap map = (ActionMap)UIManager.get("TextField.actionMap");
map.put(DefaultEditorKit.backwardAction, new MyCustomAction());

The only remaining problem now is that so far I have only gotten this to work, when I execute that code after the first instantiation of a JTextField. However, ideally I would like to integrate this code in a custom look and feel or some other central place that does not have to know about the application. Calling

UIManager.getDefaults()

first does not change anything. The map returned by UIManager.get("TextField.actionMap") is still null afterwards.

Right now I am Stuck with putting a dummy new JTextField() in some initialization method of my application and then manipulate the action map which is very ugly.

4

1 回答 1

2

共享父 actionMap 是在 XXTextComponent 的第一个实例获取其 uielegate 时创建的。该方法在 BasicTextUI 中:

/**
 * Fetch an action map to use.
 */
ActionMap getActionMap() {
    String mapName = getPropertyPrefix() + ".actionMap";
    ActionMap map = (ActionMap)UIManager.get(mapName);

    // JW: we are the first in this LAF
    if (map == null) {
        // JW: create map
        map = createActionMap();
        if (map != null) {
            // JW: store in LAF defaults
            UIManager.getLookAndFeelDefaults().put(mapName, map);
        }
    }
    ....
}

因此,如果您想添加到该地图,则必须在设置 LAF创建实例之后执行此操作。

没有办法,即使感觉很丑。

于 2013-03-23T11:00:50.503 回答