2

I have code similar to the following:

JComponent component = ...
component.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        final int button = e.getButton();
        System.out.println("button = " + button);
    }
});

I received a crash report today that was caused because e.getButton() returned 0, which is NO_BUTTON. What might be a cause? How can a mouse click be on no button?

This happened on Java 1.5.0_30 on Mac OS X 10.5.8.

The code in question has been in production for 4 years, with thousands of daily users, and this is the first time this error has appeared in a crash report.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
  • 2
    Wild guess: Is someone trying to automate usage of your tool through the Robot class?: http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html – Mathias Schwarz Sep 18 '12 at 12:44
  • 2
    I'm just guessing here but maybe it's a strange incompatibility (or bug) with the Mac multitouch trackpad or some other uncommon input device? – André Stannek Sep 18 '12 at 12:46

1 Answers1

2
  1. Java6 and newer version to supporting only Mouse (Hardware) with three buttons, there are mouse with 5buttons (on both sides, but never to tried or use that)

  2. for MouseEvents (from keyboard too) to use methods implemented into ButtonModel for JButtons JComponents

  3. JButton (maybe not valid for all JButtons JComponents) has implemented Mouse event correctly in the API, no reason multiply those events by add MouseListener, maybe there are concurency, or endles loop from attached Listeners

  4. I think NO_BUTTON can returns AbstractButton, no idea without SSCCE

  5. please is there something else for This happened on Java 1.5.0_30 on Mac OS X 10.5.8.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Also verify [*Initial Threads*](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) usage. – trashgod Sep 18 '12 at 14:10
  • ehhh .. the question is completely unrelated to JButton and buttonModel ... please remove bullets 2 - 4 (inclusive). And 5, as it is .. a question. Leaves 1 which looks like a good guess (read: would be mine as well, but wouldn't post as an answer without verifying, hint, hint ...) – kleopatra Sep 18 '12 at 15:53
  • 1. awt and swing Buttons can returns this issue, 2. caused in JButtons JComponent stored in XxxModel too, e.g. JList with JButtons in the model, 3. maybe there are another issue, – mKorbel Sep 18 '12 at 16:06
  • Point 4 is irrelevant. NO_BUTTON is an int constant identifying a mouse button. It has nothing whatsoever to do with AbstractButton. – Steve McLeod Sep 18 '12 at 16:26
  • @Steve McLeod my endless curiosity, can you reduce the `area` or type of `JComponents` that can returns `concurently events` and generating the `NO_...` from quite safe and low_level `MouseListener`, is there `consume` or `redispatch` events (from `SwingUtilities` for example) – mKorbel Sep 18 '12 at 18:03