Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JMenu keep open with right click

 
Raymond du Toit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I need help with a mouse listener on a menu I made.
I added a mouse listener to the each Menu item. If a user left clicks the menu item, it opens the option. But if he right clicks on the menu item, it should keep the menu open and display the right click popup menu. I got the popup menu to display on right click, but the menu closes, so the user cannot see which item he right clicked on.

Is it possible to override the default close operation of Jenu and keep the menu open until I left click on the menu item?

Thank You
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried it, but if you consume your event (e.consume()) it should work. From the InputEvent API (MouseEvent is a subclass):
For example, consuming mousePressed events on a Button component will prevent the Button from being activated.
 
Raymond du Toit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
e.consume() does not seem to work on JMenuItems.
It only seems to work on buttons. If I put an operation and e.consume() in one of the MouseListener methods, it seems to ignore e.consume(), executes the operation and closes the menu.

Thanx anyway!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wouldn't it be simpler to add the r/click options to the menu
as subitems (or whatever it's called - displays the > arrow)
 
Raymond du Toit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a good idea, thanx. I will try that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic