开发者

Mouse event in Java

开发者 https://www.devze.com 2023-01-04 06:40 出处:网络
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 t

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.addMous开发者_JAVA百科eMotionListener(new MouseMotionListener()
            {

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

                }

                public void mouseMoved(MouseEvent arg0)
                {

                }
            });


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消