When defining the behaviour of a simple click on a JButton, which is the right way to do it? And, what\'s the difference?
JButton but = new JButton();
but.ad
A registered ActionListener
is invoked when the Button fires an Action event, the MouseListener
is invoked when the widget detects a mouse click.
In your example both approaches show the same behaviour when you use a mouse to click on the button. But give focus to the button and press SPACE
, this should fire an action event and trigger the Action Listener but not the mouse listener.
It is advisable to use the ActionListener on Buttons, otherwise you'll not be able to control the application with a keyboard or you would be in the need to add another key event listener.