2

Consider you want to close your java application using "Close application" menu item.

3 possible solutions are (using ActionListener or MouseAdapter or MouseListener):

menuItemClose.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        System.exit(0);
    }
});

menuItemClose.addMouseListener(new MouseAdapter() {

    @Override
    public void mouseClicked(MouseEvent e) {
        System.exit(0);
    }
});

menuItemClose.addMouseListener(new MouseListener() {

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        System.exit(0);
    }
});

3 solutions, and only first one fires.

What is the explanation of this? Does some other componenets have same behavior? How to properly handle events in such cases?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Vadim
  • 1,223
  • 2
  • 17
  • 26
  • To be more precise: all mouse events are forwarded to your listener besides `mouseClicked`. – Howard Sep 24 '11 at 17:44

2 Answers2

2

In that example, you never register a KeyListener. Anyway, you should only register an ActionListener. For more information, see Handling Events from Menu Items.

See also:

mre
  • 43,520
  • 33
  • 120
  • 170
  • mre: KeyListener was a typo, I meant to write MouseListener, I updated first post, sorry for a typo. – Vadim Sep 24 '11 at 17:32
  • @Vadim, There really isn't any difference between registering a `MouseListener` and a `MouseAdapter` other than the fact that with the latter, you don't have to implement every method. – mre Sep 24 '11 at 17:33
  • Ok, thanks, I think I understand this - first is interface, and second is a class. But still, why the addMouseListener does not work? Is it due to some inter-working restrictions? I use WindowBuilder Pro in eclipse, and it adds such events to menu items, which don't work of cource. – Vadim Sep 24 '11 at 17:39
  • @Vadium, I believe it's because `JMenuItem` (or perhaps `JMenu` itself) is a compound component, therefore a certain part of the component may be catching the mouse events, which will keep you completely unaware. But I could be wrong about this... – mre Sep 24 '11 at 17:42
  • Sounds like the developers of java languare forget to propagate events from menuItems using addActionListener :). Ok, thank you for your time answering the question, will use actionListener instead. – Vadim Sep 24 '11 at 17:54
2

Sounds like the developers of Java languare forget to propagate events from menuItems using addActionListener.

No, the developers suggest that you use Action "to separate functionality and state from a component."

trashgod
  • 203,806
  • 29
  • 246
  • 1,045