2012-07-19 108 views
2

我想突出顯示屏幕上的一些元素,當PopupMenu中的一個JMenuItem被選中(鼠標懸停)時。所以我用的MouseListener我的JMenuItem與處理JPopupMenu關閉

 @Override 
     public void mouseEntered(MouseEvent e) { 
      highlightOn(); 
     } 

     @Override 
     public void mouseExited(MouseEvent e) { 
      highlightOff(); 
     } 

它工作正常,但如果我按Esc鍵彈出菜單將被關閉,沒有乾淨的highligt。我如何攔截JPopupMenu的處理結束?

回答

5

你看

  1. PopupMenuListener
更好

可能是

  1. JMenuItem(S)可以返回ButtonModel

  2. ButtonModel回到isRollover()isArmend()isPressed() e.i.

  3. 每個JButtons JComponents(JButton的,JCheckBox的,一個JRadioButton和JMenuXxx)實施diferrent方法從ButtonModel

6

像這樣的東西應該做的伎倆:

myPopupMenu.addPopupMenuListener(new PopupMenuListener { 
    public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) { 
    highlightOff(); 
    } 
    public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) { 
    highlightOff(); 
    } 
    public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {} 
} 

只記得添加監聽器在使其可見之前或在其構造函數中。

+0

+1爲PopupMenuListener,順便說一句JMenuXxx有自己的另一個Listeners,PopupMenuListener這不適用於特定的JMenuItem突出顯示....或取消選擇 – mKorbel 2012-07-19 16:06:02