Brian Green
Brian Green

Reputation: 567

Why is a WindowBuilder event not getting called?

I created a Java app in WindowBuilder for Eclipse. I built a menu and on one of the menu items I added the mouseclicked event.

JMenuItem mitemAbout = new JMenuItem("About");
    mitemAbout.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            JOptionPane.showInternalMessageDialog( form, "Message", "title", JOptionPane.PLAIN_MESSAGE);

        }
    });
    mitemHelp.add(mitemAbout);

I put a breakpoint on the JOptionPane line and when I click on the menu item in debug mode it doesn't even get to it. Am I completely missing a step here?

Upvotes: 0

Views: 317

Answers (1)

Reimeus
Reimeus

Reputation: 159844

Although JMenuItem components offer the addMouseListener method (inherited from java.awt.Component) MouseEvents are only processed for the MenuElements own functional use, i.e. any external MouseEvents will have no effect.

For JMenuItem components, use an ActionListener rather than a MouseListener to listen for user events:

mitemAbout.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
      ...               
}
});

or use:

mitemAbout.setAction(myAction);

Upvotes: 1

Related Questions