Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: how to call mouse event code

  1. #1
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default how to call mouse event code

    hi, i have the following code in a class named MouseClick:

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    Java Code:
    public class MouseClick extends MouseAdapter implements MouseListener {
    
    	public static void mouseyou() {
    
    		System.out.println("some text");
    
    	}
    
    	public void mouseClicked(MouseEvent e) {
    
    		if (e.getButton() == MouseEvent.BUTTON1) {
    
    			System.out.println("mouse clicked");
    
    			; // the first button (left?)
    
    		} else if (e.getButton() == MouseEvent.BUTTON2) {
    
    			; // the second button (centre?)
    
    		} else if (e.getButton() == MouseEvent.BUTTON3) {
    
    			; // the third button (right?)
    
    		}
    	}
    
    }
    ok, so simply, how do i run the public void mouseClicked piece of code? does it need to be a constructor?

    thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to call mouse event code

    It can't run on its own. It would need to be part of a Swing GUI where the MouseAdapter is added to some component and that component is visualized in a top-level window.

  3. #3
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: how to call mouse event code

    hello,

    so it can't be put in an *independent* method of some sort and run from there?

    thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to call mouse event code

    Quote Originally Posted by such orb View Post
    so it can't be put in an *independent* method of some sort and run from there?
    No. A Swing MouseListener and similar classes needs to be part of a Swing GUI. But you will want to go through the tutorials as they will explain all of this and more.
    DarrylBurke likes this.

Similar Threads

  1. Double-buffering and mouse=event help please
    By Parkournerd in forum New To Java
    Replies: 38
    Last Post: 01-13-2012, 01:24 PM
  2. Mouse event filtering?
    By Taiko in forum AWT / Swing
    Replies: 12
    Last Post: 08-10-2011, 02:00 PM
  3. get onbjet reference on mouse event
    By spimees in forum AWT / Swing
    Replies: 2
    Last Post: 05-18-2011, 05:28 AM
  4. popup without mouse event
    By cool in forum AWT / Swing
    Replies: 3
    Last Post: 11-04-2010, 12:57 PM
  5. Simple mouse event question
    By alacn in forum New To Java
    Replies: 13
    Last Post: 06-21-2010, 03:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •