I am sure about method actionPerformed in ActionEvent interface. What it is called? If I implement method actionPerformed and mouseClicked, when these methods execute?
Thanks.
Printable View
I am sure about method actionPerformed in ActionEvent interface. What it is called? If I implement method actionPerformed and mouseClicked, when these methods execute?
Thanks.
actionPerformed depends on the type of the attached item.
mouseClicked will run any time you click on the attached item
For example:
I used Button and implemented actionPerformed and mouseClicked.
When will actionPerformed and mouseClicked execute? When we click on a Button which one will execute?
Both will executeQuote:
Originally Posted by ychhuong
mouseClicked will execute when a button was clicked.
How about actionPerformed? When will it execute?
When you execute this code and click on the button, java prints:Code:import java.io.IOException ;
import java.awt.BorderLayout ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import java.awt.event.MouseEvent ;
import java.awt.event.MouseListener ;
import javax.swing.JButton ;
import javax.swing.JFrame ;
public class TestClass {
public static void main (String[] args) throws IOException {
JFrame f = new JFrame("Welcome") ;
JButton b = new JButton("Hi") ;
f.getContentPane().add(b, BorderLayout.CENTER) ;
b.addActionListener(new ActionListener() {
/**
* actionPerformed
*
* @param e ActionEvent
*/
public void actionPerformed (ActionEvent e) {
System.out.println("Action Performed") ;
}
}
) ;
b.addMouseListener(new MouseListener() {
/**
* mouseClicked
*
* @param e MouseEvent
*/
public void mouseClicked (MouseEvent e) {
System.out.println("Mouse Clicked") ;
}
/**
* mouseEntered
*
* @param e MouseEvent
*/
public void mouseEntered (MouseEvent e) {
}
/**
* mouseExited
*
* @param e MouseEvent
*/
public void mouseExited (MouseEvent e) {
}
/**
* mousePressed
*
* @param e MouseEvent
*/
public void mousePressed (MouseEvent e) {
System.out.println("Mouse Pressed") ;
}
/**
* mouseReleased
*
* @param e MouseEvent
*/
public void mouseReleased (MouseEvent e) {
System.out.println("Mouse Released") ;
}
}
) ;
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
f.setSize(300, 300) ;
f.setVisible(true) ;
}
}
Mouse Pressed
Action Performed
Mouse Released
Mouse Clicked
This means, Action Performed runs before the mouse clicked
I can't say this is true, but maybe it depends on the order in which you added? I mean, if you add the actionlistener first, then the actionperformed may execute first. If you add the mouselistener first, then the mouseclicked may execute first. That's just my guess, but I could very well be wrong.
No, they won't, this is not C or C++Quote:
Originally Posted by System_Error
ActionListener and MouseListener are eventHandlers, they fire when an event occurs not according to who came first, or who is faster!