我需要在獲取焦點時以編程方式爲我的button2顯示工具提示。 (I按Tab如intially聚焦在按鈕1)JButton以編程方式顯示工具提示:actionMap.get(「postTip」)爲空
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 = 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 : [email protected]
released : [email protected]
那麼,如何可以調用這個代碼,如果我得到toolTipAction空的ActionMap中的所有項目?
ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed(postTip);
爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 –