Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JMenu - Is this ok to use MouseListener over ActionListener  RSS feed

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that JMenu and JMenuItem is suppose to use action listeners but would you consider this implementation an exception?

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well one obvious drawback is that it won't be triggered if the user uses the keyboard to manipulate the menus.
What is the problem you are trying to solve that you are considering a mouse listener?
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you consider this implementation an exception?


No! Why would you ever consider this? Why would you implement 5 methods instead of a single the single "actionPerformed()" method of an ActionListener.

Do you know what a mouseClicked event is. It is a combination of mousePressed and mouseReleased at the same point. So if the user moves the mouse even 1 pixel between the two events and mouseClicked event will not be generated and the user will wonder why it doesn't work.

Always muse a more abstract API when it is available. The API was created for a reason.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
would you consider this implementation an exception?


No! Why would you ever consider this? Why would you implement 5 methods instead of a single the single "actionPerformed()" method of an ActionListener.

Do you know what a mouseClicked event is. It is a combination of mousePressed and mouseReleased at the same point. So if the user moves the mouse even 1 pixel between the two events and mouseClicked event will not be generated and the user will wonder why it doesn't work.

Always muse a more abstract API when it is available. The API was created for a reason.


If you click the link below and read the best answer on stack, you will see why I didn't use ActionListener. ActionListener doesn't register with JMenu and does absolutely nothing, do you have any other suggestions? MenuListener will not work for what I want. When I click the menu beside the one I am adding the MenuListener and then drag it to the Menu with MenuListener it just pops up my JOptionPane.showDialog, same behavior as mouse focus gained. I just need to be able to click for interaction. I will continue doing trial and errors until I come up with a solution.

Stack OverFlow
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
So if the user moves the mouse even 1 pixel between the two events and mouseClicked event will not be generated and the user will wonder why it doesn't work.


This code removes that bug.

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JMenu doesn't support AcionPerformed and mouselistener doesn't behave the way that it should to a user, I ended up using JMenu --> JMenuItem.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles

Apart from everything else, get familiar with MouseAdapter.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!