Results 1 to 5 of 5

Thread: How to write a Mouse Listener in Java?

  1. #1
    Join Date
    Jul 2006
    Posts
    128

    How to write a Mouse Listener in Java?

    I am new to this forum and hoping that i have posted my query properly. I want to use the mouse listener, so that it can be notified when the user uses the mouse. Also i know that when the user clicks or releases the mouse button, this event is occurred. But i don't know how to write a mouse listener. So please tell me how to write a Mouse Listener in Java? Hope that you guys will help me soon.!!
    Please ignore my mistakes, if any..!!
    "Every man is guilty of all the good he did not do". - Voltaire

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to write a Mouse Listener in Java?

    Hi 'PsYcHo 1',
    You are right, you will have to use the mouse listener for notifying when the user uses the mouse. You should also know that mouse events occur when the cursor enters or exits a component's onscreen area. The mouse event also takes place when the user presses or releases one of the mouse buttons. If you compare the cursor's motion and other mouse events, then the tracking the cursor's motion involves significantly more system overhead than tracking other mouse events. For more easy and better way, the mouse-motion events are separated into Mouse Motion listener type.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to write a Mouse Listener in Java?

    If your program needs to detect both mouse events and mouse-motion events, I would suggest you to use Swing's convenient MouseInputAdapter class, which implements both MouseListener and MouseMotionListener. But the MouseInputListener interface does not implement the MouseWheelListener interface. Also you will have to use the corresponding AWT MouseAdapter class, which implements the MouseListener, MouseMotionListener, and MouseWheelListener interfaces. You can also use the mouse listener on the BlankArea and on its container because it listens to both events.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: How to write a Mouse Listener in Java?

    You will understand more better after having a look to the following example which explains the mouse event handling :
    Code:
    public class MouseEventTrial ... implements MouseListener {
    
            blankArea.addMouseListener(this);
            addMouseListener(this);
        ...
    
        public void mousePressed(MouseEvent me) {
           saySomething("Mouse pressed; # of clicks: "
                        + me.getClickCount(), me);
        }
    
        public void mouseReleased(MouseEvent me) {
           saySomething("Mouse released; # of clicks: "
                        + me.getClickCount(), me);
        }
    
        public void mouseEntered(MouseEvent me) {
           saySomething("Mouse entered", me);
        }
    
        public void mouseExited(MouseEvent me) {
           saySomething("Mouse exited", me);
        }
    
        public void mouseClicked(MouseEvent e) {
           saySomething("Mouse clicked (# of clicks: "
                        + me.getClickCount() + ")", me);
        }
    
        void saySomething(String eventDescription, MouseEvent me) {
            textArea.append(eventDescription + " detected on "
                            + me.getComponent().getClass().getName()
                            + "." + newline);
        }
    }

  5. #5
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to write a Mouse Listener in Java?

    You should also know about the methods that are used by mouse listener. The following are the methods of MouseListener Interface :
    • mouseClicked(MouseEvent) - This method is called just after the user clicks the listened-to component.
    • mouseEntered(MouseEvent) - This method is called just after the cursor enters the bounds of the listened-to component.
    • mousePressed(MouseEvent) - When the user presses a mouse button while the cursor is over the listened-to component, this method is called.
    • mouseReleased(MouseEvent) - After the user releases a mouse button after a mouse press over the listened-to component, this method is called.
    • mouseExited(MouseEvent) - This method is called just after the cursor exits the bounds of the listened-to component.

Similar Threads

  1. How to write a Document Listener in Java?
    By pushpendra in forum Software Development
    Replies: 8
    Last Post: 18-03-2010, 01:22 PM
  2. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  3. How to write an Item Listener in Java?
    By shivendra in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 01:13 AM
  4. How to write a Focus Listener in Java?
    By The Recruiter in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 07:05 AM
  5. How to write a Change Listener in Java?
    By Samarth in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 06:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,031,738.53964 seconds with 17 queries