我需要在 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
使用
JWindow
(未修饰的JDialog
)或JLabel
代替ToolTip
,的例子>JLabel
by @Guillaume Polet和 here您可以将此容器放在
鼠标光标
位置或坚持绝对坐标,指向
可见的GUI标准可能(可能很烦人)参见 SystemTray and TrayIcon#displayMessage
关于java - JButton 以编程方式显示工具提示 : actionMap. get ("postTip") 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599079/