1

I need to show popup-menu on my JTree, and I added mouse listener just like that:

  tree.addMouseListener(new MouseAdapter(){
     public void mouseClicked(MouseEvent me){
        if (SwingUtilities.isRightMouseButton(me)){
           //.. some code to show popup menu
        }
     }
  });

But sometimes this event is just skipped: I clicked, but mouseClicked() is not called. The same happens with JTabbedPane: i do right click on some tab, and sometimes mouseClicked() is not called, but tab becomes actually switched.

UPD: this happens in 10% of clicks, approximately. Too often to ignore it.

By the way, I can add ChangeListener on such JTabpedPane, and this event is never skipped, but i need to handle mouse too, and I have absolutely no idea what can be wrong here.

Any help is appreciated.

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114

1 Answers1

3

I guess that the problem is that in 10% of clicks you are not actually clicking but starting and finishing a drag. That is why mouseClicked event doesn't work.

Try listening mousePressed or mouseReleased MouseAdapter's event (depends on what behavior you want) instead of mouseClicked event.

Mikle Garin
  • 10,083
  • 37
  • 59
  • It seems like your guess is quite right, thank you very much! I even thought that it is some kind of swing bug, or something. – Dmitry Frank Apr 20 '12 at 17:04