如何全局更改Swing JTextFields的ActionMap? [英] How to globally change the ActionMap of Swing JTextFields?

查看:108
本文介绍了如何全局更改Swing JTextFields的ActionMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过用自定义实现替换一些动作来更改整个应用程序中Swing JTextFields的ActionMap.有关为什么您可以参考以下帖子的说明:

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:

如何制作JTextComponent的插入符会跳过选定的文本吗?

如果我这样做,那么效果很好:

This works well if I do this:

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

现在唯一剩下的问题是,到目前为止,当我在JTextField的第一个实例化之后的 之后执行该代码时,我才开始使用它.但是,理想情况下,我希望将此代码集成到不需要了解应用程序的自定义外观或其他中央位置.打电话

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()

首先不做任何更改.此后, UIManager.get("TextField.actionMap")返回的地图仍然为空.

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

现在,我被困在应用程序的某些初始化方法中,放置了一个虚拟 new JTextField(),然后操作了非常丑陋的动作映射.

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.

推荐答案

当XXTextComponent的 first 实例获得其uidelegate时,将创建共享父actionMap.该方法在BasicTextUI中:

The shared parent actionMap is created at the moment when the first instance of a XXTextComponent gets its uidelegate. The method is in 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 之后执行此操作.

So if you want to add to that map, you have to do so after setting a LAF and after creating an instance.

即使感觉很难看,也无法解决.

There's no way around, even though it feels ugly.

这篇关于如何全局更改Swing JTextFields的ActionMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆