|
-
Mar 14th, 2006, 10:46 PM
#1
Thread Starter
Addicted Member
Different between actionPerformed and mouseClicked event
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.
-
Mar 15th, 2006, 03:44 AM
#2
Re: Different between actionPerformed and mouseClicked event
actionPerformed depends on the type of the attached item.
mouseClicked will run any time you click on the attached item
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 15th, 2006, 08:49 PM
#3
Thread Starter
Addicted Member
Re: Different between actionPerformed and mouseClicked event
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?
-
Mar 16th, 2006, 12:12 AM
#4
Re: Different between actionPerformed and mouseClicked event
 Originally Posted by ychhuong
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 execute
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 16th, 2006, 03:50 AM
#5
Thread Starter
Addicted Member
Re: Different between actionPerformed and mouseClicked event
mouseClicked will execute when a button was clicked.
How about actionPerformed? When will it execute?
-
Mar 16th, 2006, 01:18 PM
#6
Re: Different between actionPerformed and mouseClicked event
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) ;
}
}
When you execute this code and click on the button, java prints:
Mouse Pressed
Action Performed
Mouse Released
Mouse Clicked
This means, Action Performed runs before the mouse clicked
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 16th, 2006, 08:30 PM
#7
Frenzied Member
Re: Different between actionPerformed and mouseClicked event
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.
-
Mar 17th, 2006, 05:09 AM
#8
Re: Different between actionPerformed and mouseClicked event
 Originally Posted by System_Error
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++
ActionListener and MouseListener are eventHandlers, they fire when an event occurs not according to who came first, or who is faster!
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|