views:

31

answers:

1

I am trying to move a JComponent say a label over a table.I am tracking this event using MouseMotionListener's mouseDragged method.This method perfectly helps me in tracking the item.Is there a way to track the mouse release after dragging is complete(.ie the dropping event).

 tktLabel1.addMouseMotionListener(new MouseMotionListener()
            {

                public void mouseDragged(MouseEvent arg0)
                {
                    tktLabel1.setBounds(tktLabel1.getX() + arg0.getX(),
                            tktLabel1.getY() + arg0.getY(), width, height);

                }

                public void mouseMoved(MouseEvent arg0)
                {

                }
            });
+4  A: 

There are 2 listeners for mouse events. The MouseMotionListener which you are already using and the MouseListener, which listens for such things as pressed, released etc.

If it is too much of a burden to implement all six methods on this interface you can extend the MouseAdapter instead which provides default no op methods for all the event types and you can just override the ones you need.

EDIT

It seems on closer inspection that JList, JTable and JTree require a bit extra for drag and drop support. You will have to implement a DropTarget to be notified of these events.

krock