I searched the web for examples of draggable Swing components, but I found either incomplete or non-working examples.
What I need is a Swing component that can be dragged by the mouse inside an other component. While being dragged, it should already change its position, not just 'jump' to its destination.
I would appreciate examples which work without non-standard APIs.
Thank you.
addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent ev){ x = ev. getX (); y = ev. getY(); } }); //.... //on mouse dragged jpanel. addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { int x = evt.
The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location).
Drag and drop enables data transfer across Java programming language and native applications, between Java programming language applications, and within a single Java programming language application.
I propose a simple, but well-working solution, found out by myself ;)
What do I do?
Tested with latest JDK 6 unter Linux (OpenSuse, KDE3),
but hey, it's Java Swing, should work equally everywhere.
Here goes the code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class MyDraggableComponent extends JComponent { private volatile int screenX = 0; private volatile int screenY = 0; private volatile int myX = 0; private volatile int myY = 0; public MyDraggableComponent() { setBorder(new LineBorder(Color.BLUE, 3)); setBackground(Color.WHITE); setBounds(0, 0, 100, 100); setOpaque(false); addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { screenX = e.getXOnScreen(); screenY = e.getYOnScreen(); myX = getX(); myY = getY(); } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }); addMouseMotionListener(new MouseMotionListener() { @Override public void mouseDragged(MouseEvent e) { int deltaX = e.getXOnScreen() - screenX; int deltaY = e.getYOnScreen() - screenY; setLocation(myX + deltaX, myY + deltaY); } @Override public void mouseMoved(MouseEvent e) { } }); } } public class Main { public static void main(String[] args) { JFrame f = new JFrame("Swing Hello World"); // by doing this, we prevent Swing from resizing // our nice component f.setLayout(null); MyDraggableComponent mc = new MyDraggableComponent(); f.add(mc); f.setSize(500, 500); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setVisible(true); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With