• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mouse motion problems

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to build some sort of floating panel, the panel is a JWindow and I need it draggable.For this I use a MouseMotionAdapter ,I override the mouseDragged method like :
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
floatPanel.setLocation(e.getPoint());
}

the floatPanel is my JWindow.The problem is the panel jumps over the screen ,see the output :

>=>java.awt.Point[x=2,y=5]
>=>java.awt.Point[x=5,y=9]
>=>java.awt.Point[x=119,y=19]
>=>java.awt.Point[x=4,y=9]
>=>java.awt.Point[x=3,y=7]
>=>java.awt.Point[x=123,y=26]
>=>java.awt.Point[x=9,y=15]
>=>java.awt.Point[x=120,y=22]
>=>java.awt.Point[x=122,y=24]
>=>java.awt.Point[x=4,y=8]

I think that the after after the mouse moves, the new location for the panel is set and this generate a new event (the small values).Why ?
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I have it.The position is relative to the floating panel(it has the mouse listener), if it moves the postion on screen must be calculate relaive to its last position.
See the example :

public void mouseReleased(MouseEvent e) {
Point mouse = e.getPoint();

if(lastPos == null) {
lastPos = mouse;
} else {
lastPos.translate(mouse.x, mouse.y);
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
floatPanel.setLocation(lastPos);
}
});

super.mouseReleased(e);
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!