java - JButton 以编程方式显示工具提示 : actionMap. get ("postTip") 为空

标签 java swing tooltip jbutton focuslistener

我需要在 button2 获得焦点时以编程方式显示工具提示。 (我按 Tab 键是因为 initially focus at button1)

            JButton button = new JButton("Button 1");

            JButton button2 = new JButton("Button 2");
            button2.setToolTipText("tooltip2");
            button2.addFocusListener(new FocusListener());

我指的是 code通过@camickr

private class FocusListener extends FocusAdapter {
 public void focusGained(FocusEvent e)
 {
    JComponent component = (JComponent)e.getSource();
    Action toolTipAction = component.getActionMap().get("postTip");

但 toolTipAction 设置为空。

我已经使用这段代码打印了 ActionMap 的所有条目

        ActionMap actionMap = component.getActionMap();
        Object[] actionMapKeys = actionMap.allKeys();

        for (int i = 0; i < actionMapKeys.length; i++) {
            Object key = actionMapKeys[i];
            System.out.println(key.toString() + " : " + actionMap.get(key).toString());
        }

这就是它带给我的

pressed : javax.swing.plaf.basic.BasicButtonListener$Actions@49cf9f
released : javax.swing.plaf.basic.BasicButtonListener$Actions@1de0b5e

那么,如果我的 toolTipAction 为 null,我该如何调用这段代码呢?

ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed(postTip);

最佳答案

actuall I want to show tooltip on some network event, which has nothing to do with mouse. but I have started experiments with focus gained by Tab key

关于java - JButton 以编程方式显示工具提示 : actionMap. get ("postTip") 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599079/

相关文章:

java - 是否有用于添加 JBoss AS 6 支持的 NetBeans 插件?

java - 替换 spring/hibernate 项目中的 import.sql 文件

java - 时间序列JFreeChart重绘时间轴

java - 如何在netbeans平台中制作 "Open File..."窗口?

银光: "The name already exists in the tree"

javascript - Highcharts 列工具提示 - 始终位于顶部 + 适合容器

Java 远程接口(interface)阻止使用我的 GUI

java - 用 Java 解析 CSV 文件

java - 如何设置按钮上的文字颜色? java

c++ - 如何自定义工具提示的外观? (C++ WinAPI)