如何全局更改Swing JTextFields的ActionMap?

时间:2013-03-23 07:54:50

标签: java swing jtextfield jtextcomponent uimanager

我希望通过我的自定义implmentations替换一些操作来更改整个Application中的Swing JTextFields的ActionMap。有关解释原因,请参阅以下帖子:

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

如果我这样做,这很有效:

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

现在唯一剩下的问题是,到目前为止,只有在第一次实例化JTextField后执行代码时,我才能使用它。但是,理想情况下,我希望将此代码集成到自定义外观或其他一些不必了解应用程序的中心位置。调用

UIManager.getDefaults()

首先不改变任何东西。之后, UIManager.get(“TextField.actionMap”)返回的地图仍然为空。

现在我被困在我的应用程序的某些初始化方法中放入一个虚拟的 new JTextField(),然后操纵非常难看的动作图。

1 个答案:

答案 0 :(得分:2)

共享父actionMap是在XXTextComponent的第一个实例获取其uidelegate时创建的。该方法在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 之后执行此操作。

没有办法,即使感觉很难看。