Difference between ActionPerformed and MouseClicked Java

0

I'm starting to see the graphic interface issue in java, and I notice that if an event is assigned to a JButton example: MouseClicked is a different method of ActionPerformed , What is the difference between this and any other event assigned to it? What happens if no event is assigned to the button?

    
asked by Shiro 08.09.2017 в 20:08
source

2 answers

2

First, it is important to be precise to avoid confusion. An "event is not assigned" to a component, it is assigned a listener that the component accepts. The listener will have several methods defined that the component will invoke when it needs to notify an event.

mouseClicked() is a method of MouseListener , actionPerformed() is a method of ActionListener . The two classes have different functions; MouseListener deals with low level events (what is done with the mouse), the events of ActionListener are more at high level. For example, when you click on a button, the method mouseClicked of the MouseListener associated% will be invoked, and the method actionPerformed() of the ActionListener . But if you navigate with the tab and on the button press Enter, it will not call mouseClicked() but yes% actionPerformed() .

What happens if a component does not have assigned listeners ? It will not call any method when it detects the event, no logic will be executed.

    
answered by 08.09.2017 в 20:18
0

ActionPerformed (ActionEvent e) is an ActionListener event. ActionListener is used to detect and handle events, which take place when an action occurs on an element of the program.

In other words when talking about ActionPerformed we refer to the event that occurs when you click on a component or when you press Enter (with the focus on the component). While a MouseClicked, as the name implies, is more specific to being an event that comes into action (ONLY) when clicking on an item.

On the other hand, if you do not add any events to the component, the latter will not take any action

    
answered by 08.09.2017 в 20:45