Mouse event in Java

♀尐吖头ヾ 提交于 2019-12-11 02:35:31

问题


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)
                {

                }
            });

回答1:


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.



来源:https://stackoverflow.com/questions/3060538/mouse-event-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!