static A draggableFloatingComponent(final A a) { final new Var pt; // where in the component mouse was clicked addMouseListener(a, new MouseAdapter { public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) pt.set(pt(e.getX(), e.getY())); } public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { //a.setLocation(e.getX()+pt->x, e.getY()+pt->y); pt.set(null); } } }); addMouseMotionListener(a, new MouseMotionAdapter { public void mouseDragged(MouseEvent e) { Point scr = e.getLocationOnScreen(); Point p = a.getParent().getLocationOnScreen(); a.setLocation(scr.x-pt->x-p.x, scr.y-pt->y-p.y); } }); ret a; }