简体   繁体   中英

I want to perform some action when a mouse is over JMenuItem. What listener should I use?

I want to perform some action when a mouse is over JMenuItem. What listener should I use?

Use MouseListener . Its method mouseEntered() and mouseExited() will be helpful to you.

If 'some action' happens to be 'show a message', look at JComponent.setToolTipText(String) .

and alternative is

    menuItem1.getModel().addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            ButtonModel model = (ButtonModel) e.getSource();
            if (model.isRollover()) {
                // some stuff
            }// may be another states from ButtonModel
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM