title: java move components with mouse
body: <p>I tried to make any Component draggable by simply adding mouse listeners and using the <code>setLocation</code> function of <code>java.awt.Component</code>. I started with <code>JButton</code> to test if it were possible the way i thought.</p>
  
  <p>Here is a code example for what I am trying to do:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  
  public class DragButton extends JButton{
  
  private volatile int draggedAtX, draggedAtY;
  
  public DragButton(String text){
      super(text);
      setDoubleBuffered(false);
      setMargin(new Insets(0, 0, 0, 0));
      setSize(25, 25);
      setPreferredSize(new Dimension(25, 25));
  
      addMouseListener(new MouseAdapter(){
          public void mousePressed(MouseEvent e){
              draggedAtX = e.getX() - getLocation().x;
              draggedAtY = e.getY() - getLocation().y;
          }
      });
  
      addMouseMotionListener(new MouseMotionAdapter(){
          public void mouseDragged(MouseEvent e){
              setLocation(e.getX() - draggedAtX, e.getY() - draggedAtY);
          }
      });
  }
  
  public static void main(String[] args){
      JFrame frame = new JFrame("DragButton");
      frame.setLayout(null);
      frame.getContentPane().add(new DragButton("1"));
      frame.getContentPane().add(new DragButton("2"));
      frame.getContentPane().add(new DragButton("3"));
      frame.setSize(300, 300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>Somehow this fails to work properly and I don't get why. The actual distance dragged is half the distance of the mouse movement and it flickers around that distance while dragging as if two mouse positions are competing over the <code>MouseMotionListener</code>.</p>
  
  <p>May anyone help a swing/awt noob? =)
  Many thanks in advance.</p>
  
  <p>Edit:</p>
  
  <p>Ok, so the problem was that I did not know that the event would refire at each mouse location with the position being <strong>relative</strong>(!) to the firing <code>JComponent</code>. So this is the corrected and working code:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class DragButton extends JButton{
  
      private volatile int draggedAtX, draggedAtY;
  
      public DragButton(String text){
          super(text);
          setDoubleBuffered(false);
          setMargin(new Insets(0, 0, 0, 0));
          setSize(25, 25);
          setPreferredSize(new Dimension(25, 25));
  
          addMouseListener(new MouseAdapter(){
              public void mousePressed(MouseEvent e){
                  draggedAtX = e.getX();
                  draggedAtY = e.getY();
              }
          });
  
          addMouseMotionListener(new MouseMotionAdapter(){
              public void mouseDragged(MouseEvent e){
                  setLocation(e.getX() - draggedAtX + getLocation().x,
                          e.getY() - draggedAtY + getLocation().y);
              }
          });
      }
  
      public static void main(String[] args){
          JFrame frame = new JFrame("DragButton");
          frame.setLayout(null);
          frame.getContentPane().add(new DragButton("1"));
          frame.getContentPane().add(new DragButton("2"));
          frame.getContentPane().add(new DragButton("3"));
          frame.setSize(300, 300);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>Thanks Adel for your efforts and mKorbel for the link.</p>