2012-11-28 55 views
1

我需要在獲取焦點時以編程方式爲我的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); 
+0

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 –

回答

2

actuall我想顯示一些網絡事件的工具提示,其中有 與鼠標無關。但我已經開始通過Tab鍵

  • 使用JWindow(未修飾的JDialog)或JLabel代替ToolTip,實例獲得焦點 實驗的JLabelby @Guillaume Polet和​​

  • 你可以躺在這個容器到Mouse Cursor位置或堅持絕對座標,Point到可見的GUI

  • 標準可以是(可惱人)看到SystemTray and TrayIcon#displayMessage

+0

獲得的焦點實驗,聽起來不錯,我會研究這個! –

+1

@NikolayKuznetsov你也可以看看[這個答案](http://stackoverflow.com/questions/12822819/dynamically-update-tooltip-currently-displayed/12823177#12823177)我在哪裏自動使工具提示出現在組件,雖然我沒有任何實際的MouseEvent(我們動態地創建一個假的):'ToolTipManager.sharedInstance()。mouseMoved( )新的MouseEvent(組件,-1,System.currentTimeMillis(),0,locationOnComponent.x,locationOnComponent 。y, locationOnScreen.x,locationOnScreen.y,0,false,0)); –

2

您也可以嘗試配置工具提示經理立即顯示工具提示輸入鼠標時的另一種方法。

javax.swing.ToolTipManager.sharedInstance().setInitialDelay(0) 

如果您只想爲某些組件發生這種情況,您可以根據獲取焦點的組件更改此值。

當按下Ctrl+F1時,也會顯示工具提示。當你想顯示工具提示時,你可以在按鈕上使用java.awt.Robot來模擬Ctrl + F1。

+0

actuall我想顯示一些網絡事件的工具提示,這與鼠標無關。但我已經開始通過Tab key –

相關問題