Results 1 to 8 of 8

Thread: Different between actionPerformed and mouseClicked event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Question 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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Question 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?

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Different between actionPerformed and mouseClicked event

    Quote 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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Question Re: Different between actionPerformed and mouseClicked event

    mouseClicked will execute when a button was clicked.
    How about actionPerformed? When will it execute?

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Different between actionPerformed and mouseClicked event

    Quote 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
  •  



Click Here to Expand Forum to Full Width