Difference between mouse listener and event executor?

What is the difference? When will you use the mouse listener? or action listener? Please and thanks!

+5
source share
3 answers

ActionListener Doc

The listener interface for receiving action events. A class that is interested in handling the event of the event implements this interface, and an object created using this class is registered using the components using the addActionListener component method. When an action event occurs, this object actionPerformed method.

MouseListener Doc

The listener interface for receiving "interesting" mouse events (click, release, click, enter and exit) on the component. (To track mice and muscles, use MouseMotionListener .)

In the docs, you see that using these interfaces is completely different. Although you can only use MouseListener in conjunction with gui elements, the ActionListener also used when there is no gui, for example, in combination with a timer.

+3
source

The first difference is that A MouseEvent is a true system event, while an ActionEvent is a synthesized event ... It is triggered by a system event.

MouseListener (and MouseMotionLister, MouseWheelListener) are useful when (a) you are interested in the details of an event (i.e. the location of the x / y click) or when the component you are using does not support ActionListeners

Action good when you have a task that can be performed without external events (for example, exiting a program) and that you want to have access to more than one component or to start / run using the keyboard or mouse

+2
source

ActionListener is used to handle logical button clicks. Click occurs:

  • when the mouse is pressed, and then release the button,
  • or when the key combination of this button is used,
  • or when the button has focus and space, press
  • or when the button is the default button and the "Enter" key is pressed,
  • or when the click () method is called programmatically

MouseListener only processes events at the lower level.

+2
source

All Articles