Reputation: 7509
I want to highlight some elements on screen, when one JMenuItem in PopupMenu ist selected(mouse over). So i use MouseListener on my JMenuItem with
@Override
public void mouseEntered(MouseEvent e) {
highlightOn();
}
@Override
public void mouseExited(MouseEvent e) {
highlightOff();
}
It works fine, but if i press Esc the popup menu will be closed, without clean the highligt. How can i intercept the closing of JPopupMenu to handle this?
Upvotes: 2
Views: 2970
Reputation: 17955
Something like this should do the trick:
myPopupMenu.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuCanceled(final PopupMenuEvent e) {
highlightOff();
}
@Override
public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
highlightOff();
}
@Override
public void popupMenuWillBecomeVisible(final PopupMenuEvent e) { }
}
Just remember to add the listener before you make it visible, or inside its constructor.
Upvotes: 7
Reputation: 109823
you look at
PopupMenuListener
better could be
JMenuItem
(s) can returns ButtonModel
,
ButtonModel
returns isRollover()
, isArmend()
, isPressed()
e.i.
each of JButtons JComponents
(JButton, JCheckBox, JRadioButton and JMenuXxx) implemented diferrent methods from ButtonModel
Upvotes: 4