Moving Panel with mouse

I am making a Panel which can move with mouse dragging. But when I put the panel on a JTabbedPane a strange thing happens.

After I drag the panel to a new place, then I just click on another area within the JTabbedPane, or the JTabbedPane name, the Panel moves back to its original place.

Please, anyone can find my mistake ? Thank you.

import java.awt.;
import java.awt.
;
import java.awt.event.;
import javax.swing.
;
import javax.swing.border.*;

public class MovingPanel extends JFrame
{
JPanel p;
Point pPoint;
MouseEvent pressed;

public MovingPanel()
{
p = new JPanel();
p.setBackground(Color.cyan);
p.setBorder(new LineBorder(Color.black));
p.setSize(20, 20);

p.addMouseListener(new MouseAdapter()
  {public void mousePressed(MouseEvent e)
  {
    if (e.getSource() == p)
    {
      pressed = e;
    }
  }
  });

p.addMouseMotionListener(new MouseMotionAdapter()
  {public void mouseDragged(MouseEvent e)
  {
    if (e.getSource() == p)
    {
      pPoint = p.getLocation(pPoint);
      int x = pPoint.x - pressed.getX() + e.getX();
      int y = pPoint.y - pressed.getY() + e.getY();
      p.setLocation(x,y);
    }
  }
  });


p.setBorder(new LineBorder(Color.black));
p.add("Center", new JLabel("To Move"));

JPanel a = new JPanel();
a.add(p);

JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab(" Layout Definition ", a);
jTabbedPane.setSize(100, 100);

JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.blue);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(jTabbedPane, BorderLayout.CENTER);

JPanel mainPanel2 = new JPanel();
mainPanel2.add(new JLabel("TEst"));

getContentPane().add("Center", mainPanel);
getContentPane().add("East", mainPanel2);
setSize(300,300);
setVisible(true);

}

public static void main (String[] args)
{
MovingPanel mainPanel = new MovingPanel();
mainPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

It is OK when the moving panel is not on a TabbedPane, but the same thing happens when I resize the window.

import java.awt.;
import java.awt.
;
import java.awt.event.;
import javax.swing.
;
import javax.swing.border.*;

public class MovingPanel extends JFrame
{
JPanel p;
Point pPoint;
MouseEvent pressed;

public MovingPanel()
{
p = new JPanel();
p.setBackground(Color.cyan);
p.setBorder(new LineBorder(Color.black));
p.setSize(20, 20);

p.addMouseListener(new MouseAdapter()
  {public void mousePressed(MouseEvent e)
  {
    if (e.getSource() == p)
    {
      pressed = e;
    }
  }
  });

p.addMouseMotionListener(new MouseMotionAdapter()
  {public void mouseDragged(MouseEvent e)
  {
    if (e.getSource() == p)
    {
      pPoint = p.getLocation(pPoint);
      int x = pPoint.x - pressed.getX() + e.getX();
      int y = pPoint.y - pressed.getY() + e.getY();
      p.setLocation(x,y);
    }
  }
  });


p.setBorder(new LineBorder(Color.black));
p.add("Center", new JLabel("To Move"));

JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.blue);
mainPanel.add(p, BorderLayout.CENTER);

JPanel mainPanel2 = new JPanel();
mainPanel2.add(new JLabel("TEst"));

getContentPane().add("Center", mainPanel);
getContentPane().add("East", mainPanel2);
setSize(300,300);
setVisible(true);

}

public static void main (String[] args)
{
MovingPanel mainPanel = new MovingPanel();
mainPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Anyone have a good solution for me ? Thanks a lot.

The Container to which you add Panel must have its layout managed set to null, otherwise, as soon as the Component is repainted it will be moved back to where the LayoutManaged wants it.

so, in your code, you should add :-

a.setLayout(null);

Also, because you have disabled the layout managed you may well have to explicity set the initial x,y,width and height of the JPanel p.