Results 1 to 5 of 5

Thread: What is Mouse-Motion Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    222

    What is Mouse-Motion Listener in Java?

    Hi friends,
    I have tried to do the mouse listener in Java, and I was somewhat successful in using that. But I want to use the Mouse-Motion, which I tried a lot but was not getting the desired results. So can anyone please tell me how to use the Mouse-Motion Listener in Java? Please help me soon as possible.!!
    Just a reply to say thank you for these links and posts I have a lot to read and learn now!



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

    Re: What is Mouse-Motion Listener in Java?

    I guess that you are not clear with the concepts of the mouse motion listener. So I am providing you with the same information, so that you can understand it properly. The mouse-motion events are used when you want to check the user uses the mouse to move the onscreen cursor. This listener can also be used while using the similar input device. If you are making an application that requires the detection of both mouse events and mouse-motion events, then you will have to use the MouseInputAdapter class.

  3. #3
    Join Date
    Mar 2008
    Posts
    349

    Re: What is Mouse-Motion Listener in Java?

    I have provided you with the coding that can be used for implementing the mouse-motion event handling :
    Code:
    public class MouseMotionEventTrial extends JPanel 
                                      implements MouseMotionListener {
    
            blankArea.addMouseMotionListener(this);
            addMouseMotionListener(this);
            ...
        }
    
        public void mouseMoved(MouseEvent me) {
           saySomething("Mouse moved", me);
        }
    
        public void mouseDragged(MouseEvent me) {
           saySomething("Mouse dragged", me);
        }
    
        void saySomething(String eventDescription, MouseEvent e) {
            textArea.append(eventDescription 
                            + " (" + me.getX() + "," + me.getY() + ")"
                            + " detected on "
                            + me.getComponent().getClass().getName()
                            + newline);
        }
    }

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

    Re: What is Mouse-Motion Listener in Java?

    Many times you need to extend MouseInputAdapter when the handler doesn't implement any listener interface directly. The following coding may be useful in that case :
    Code:
    MyListener myListener = new MyListener();
        addMouseListener(myListener);
        addMouseMotionListener(myListener);
    ...
    private class MyListener extends MouseInputAdapter {
        public void mousePressed(MouseEvent me) {
            int x = me.getX();
            int y = me.getY();
            currentRect = new Rectangle(x, y, 0, 0);
            updateDrawableRect(getWidth(), getHeight());
            repaint();
        }
    
        public void mouseDragged(MouseEvent me) {
            updateSize(me);
        }
    
        public void mouseReleased(MouseEvent me) {
            updateSize(me);
        }
    
        void updateSize(MouseEvent me) {
            int x = me.getX();
            int y = me.getY();
            currentRect.setSize(x - currentRect.x,
                                y - currentRect.y);
            updateDrawableRect(getWidth(), getHeight());
            Rectangle totalRepaint = rectToDraw.union(previouseRectDrawn); 
            repaint(totalRepaint.x, totalRepaint.y,
                    totalRepaint.width, totalRepaint.height);
        }
    }

  5. #5
    Join Date
    Aug 2006
    Posts
    235

    Re: What is Mouse-Motion Listener in Java?

    The following sample of coding would be useful for creating the GUI, in which method is invoked from the event-dispatching thread. Here is the code for the same :
    Code:
    private static void TrialGUI() {
            JFrame frame = new JFrame("MouseMotionTrial");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JComponent newContentPane = new MouseMotionTrial();
            newContentPane.setOpaque(true);
            frame.setContentPane(newContentPane);
            
            frame.pack();
            frame.setVisible(true);
        }
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

Similar Threads

  1. Own Event Listener in Java
    By Messenger in forum Software Development
    Replies: 6
    Last Post: 13-08-2010, 10:20 AM
  2. How to write a Document Listener in Java?
    By pushpendra in forum Software Development
    Replies: 8
    Last Post: 18-03-2010, 01:22 PM
  3. How to write a Mouse Listener in Java?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 03:47 AM
  4. 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
  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,751,682,000.34363 seconds with 16 queries