MouseMotionListener not Firing correctly in Java

can anyone tell me why the mouseMotionListener is not firing the mouseDragged event(I have googled for hours and even copy-and-pasted code from the net!) Below is the code for the class - I create an object of the class and add it to a JPanel called canvas.

PS the mousePressed() is the only method that gets fired, mouseDragged() and mouseReleased(), they do not.

class MouseActions extends MouseInputAdapter 
{
        @Override
        public void mousePressed(MouseEvent e) 
        {
            super.mousePressed(e);//245 220
            java.awt.Point Pos = e.getPoint();
            System.out.println("at Mouse Pressed, Again");
            if(e.getButton() == MouseEvent.BUTTON3)
            {
                if(ArrayOfShapes == null)
                    return;

                for(int i = 0; i < ArrayOfShapes.length; i++)
                {
                    if(hasEntered(ArrayOfShapes[i], Pos))
                    {
                        removeAtIndex(i);
                        return;
                    }
                }
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) 
        {
           System.out.println("at Mouse Dragged");
            int MovableIndex = -1;
            java.awt.Point Pos = e.getPoint();

            if(e.getButton() == MouseEvent.BUTTON1)
            {
                bDragged = true;

                while(bDragged)
                {
                    for(int i = 0; i < ArrayOfShapes.length; i++)
                    {
                        if(hasEntered(ArrayOfShapes[i], Pos))
                        {
                            MovableIndex = i;
                            break;
                        }
                    }
                    ArrayOfShapes[MovableIndex].setX(e.getX());
                    ArrayOfShapes[MovableIndex].setY(e.getY());
                    thisCurrentWindow.repaint();
                }
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) 
        {
            System.out.println("at Mouse Release");
            bDragged = false;
        }

    }

/// Now the code to add the listeners

MouseActions MA = new MouseActions();
canvas.addMouseListener(MA);
canvas.addMouseMotionListener(MA);

Again Thanks alot~

M

PS.... For all who are doubting my mad inheritance skills

import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;


public class CustomJPanel extends JPanel
{
    class MouseActions extends MouseInputAdapter
    {

        @Override
        public void mousePressed(MouseEvent e) 
        {
            super.mousePressed(e);
            System.out.println("Pressed");
        }

        @Override
        public void mouseDragged(MouseEvent e) 
        {
            super.mouseDragged(e);
            System.out.println("Dragged");
        }

        @Override
        public void mouseReleased(MouseEvent e) 
        {
            super.mouseReleased(e);
            System.out.println("Released");
        }
    }

    /**
     * @param args the command line arguments
     */

    private CustomJPanel()
    {
        MouseActions ma = new MouseActions();
        addMouseListener(ma);
        addMouseMotionListener(ma);
    }

    public static void main(String[] args) 
    {
        // TODO code application logic here
        JFrame frame = new JFrame();
        CustomJPanel cP = new CustomJPanel();

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cP.setSize(500, 500);
        frame.add(cP);
        frame.setVisible(true);
    }
}

Again when I assign this to canvas it never fires the release or the dragged

M

Answers


The loop while(bDragged) looks like an infinite loop


As far I can see you need to implement the mouse listener. Referring to: http://profs.etsmtl.ca/mmcguffin/learn/java/04-mouseInput/


Scary enough - i worked just the way it was coded to.... Except later in the code i removed the canvas and recreated it - but forgetting to reassign the MIA.

Thanks for all your help,

M


I would do this:

class MouseActions implements MouseMotionListener, MouseListener
{
..
..
}

canvas.addMouseListener(new MouseActions ());
canvas.addMouseMotionListener(new MouseActions() );

and that would probably work.


Need Your Help

Programmatically create MS SQL Compact database file on a desktop

.net sql-server windows-mobile compact-database

Is it possible to create an SDF database file for MS SQL Server Compact on a desktop.?