AvrDragon
AvrDragon

Reputation: 7509

Handle JPopupMenu closed

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

Answers (2)

tucuxi
tucuxi

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

mKorbel
mKorbel

Reputation: 109823

you look at

  1. PopupMenuListener

better could be

  1. JMenuItem(s) can returns ButtonModel,

  2. ButtonModel returns isRollover(), isArmend(), isPressed() e.i.

  3. each of JButtons JComponents (JButton, JCheckBox, JRadioButton and JMenuXxx) implemented diferrent methods from ButtonModel

Upvotes: 4

Related Questions