Reputation: 12453
I have a JButton, and would like to capture mouse clicks on it. What are the practical and philosophical differences between using an ActionListener vs. using a MouseListener on the JButton ?
Upvotes: 11
Views: 8074
Reputation: 2349
Fundamentally, MouseListener is for picking up arbitary clicks. ActionListener is for picking up "someone actioning the button". So if you're really interested in the buton being activated use ActionListener. That way you'll get the event if it's activated via the keyboard or any other mechanism.
MouseListener on the other hand should be used if you're interested specifically in the click. E.g. which part of the button did they click on, did they click on something that's not activatable etc.
Upvotes: 3
Reputation: 25613
If you just want to know that the button has been pressed use ActionListener
. If your checks involve deeper analysis like mouse state (mouse entered the button, exited) etc, use MouseListener
Upvotes: 4
Reputation: 691765
An ActionListener is used to handle the logical click of a button. A click happens
A MouseListener only handles low-level mouse events.
Upvotes: 16