Q:

Drag and Drop of JPanel wont drop

I'm basically trying to drag and drop a JPanel which contains a  Jlabel:
Here is a sample of the drag and drop code I have implemented:
public class TestDragAndDropPanel extends JPanel{

private static final long serialVersionUID = 1L;
Border borderContainer = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK);
private DataFlavor dataFlavor = new DataFlavor(TestDragAndDropPanel.class, "TestDragAndDropPanel");

public TestDragAndDropPanel(){
            JPanel mainDragPanel = new JPanel();
    mainDragPanel.setBorder(borderContainer);

    JPanel mainDropPanel = new JPanel();
    mainDropPanel.setBorder(borderContainer);

    JPanel draggablePanel = new JPanel();
    draggablePanel.setBorder(borderContainer);
    JLabel draggableTitle = new JLabel("This is Draggable");
    JLabel draggableTitle2 = new JLabel("Another Component");
    draggablePanel.add(draggableTitle);
    draggablePanel.add(draggableTitle2);

    JPanel droppablePanel = new JPanel();
    droppablePanel.setBorder(borderContainer);
    JLabel droppableTitle = new JLabel("This is Droppable");
    JLabel droppableTitle2 = new JLabel("Another Component 2");
    droppablePanel.add(droppableTitle);
    droppablePanel.add(droppableTitle2);

    mainDragPanel.add(draggablePanel);
    mainDropPanel.add(droppablePanel);
    this.add(mainDragPanel);
    this.add(mainDropPanel);

    init(mainDropPanel, draggablePanel);
}

private void init(JPanel mainDropPanel, JPanel draggablePanel) {
    DragSource ds = new DragSource();
    ds.createDefaultDragGestureRecognizer(draggablePanel, DnDConstants.ACTION_MOVE, new DragGestureListImp());

    new MyDropTargetListImp(mainDropPanel);
}

private class TransferablePanel implements Transferable { 

    private JPanel transferablePanel;

    public TransferablePanel(JPanel transferablePanel) {
        this.transferablePanel = transferablePanel;
    } 

    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] { dataFlavor };
    }

    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(dataFlavor);
    }

    @Override
    public Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException, IOException {

        if (flavor.equals(dataFlavor)){
            return transferablePanel;
        } else {
            throw new UnsupportedFlavorException(flavor);
        }
    }
}

private class DragGestureListImp implements DragGestureListener {

    @Override
    public void dragGestureRecognized(DragGestureEvent event) {
        Cursor cursor = null;
        JPanel panel = (JPanel) event.getComponent();

        if (event.getDragAction() == DnDConstants.ACTION_MOVE) {
            cursor = DragSource.DefaultMoveDrop;
        }
        event.startDrag(cursor, new TransferablePanel(panel));
    }
}

private class MyDropTargetListImp extends DropTargetAdapter implements DropTargetListener {

    private DropTarget dropTarget;
    private JPanel panel;

    public MyDropTargetListImp(JPanel panel) {
        this.panel = panel;
        dropTarget = new DropTarget(panel, DnDConstants.ACTION_MOVE, this,
                true, null);
    }

    public void drop(DropTargetDropEvent event) {
        try {
            Transferable tr = event.getTransferable();
            JPanel an = (JPanel) tr.getTransferData(dataFlavor);

            if (event.isDataFlavorSupported(dataFlavor)) {
                event.acceptDrop(DnDConstants.ACTION_MOVE);
                this.panel.add(an);
                event.dropComplete(true);
                this.panel.validate();
                this.panel.repaint();
                return;
            }
            event.rejectDrop();
        } catch (Exception e) {
            e.printStackTrace();
            event.rejectDrop();
        }
    }
}

public static void main(String[] args){
    System.out.println("Application Running");
    JPanel testDragAndDropPanel =  new TestDragAndDropPanel();

    JFrame mainframe =  new JFrame();
    mainframe.add(testDragAndDropPanel);
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainframe.setSize(300,200);
    mainframe.setVisible(true);
}
}

Basically, nothing seems to be happening on the drop side, could anyone tell me why?
Thanks,

A:

Are you sure nothing happens? If you maximized your window after dropping the panel then you'd see the change. The problem is that you're not revalidating the drop panel.
In your drop method, replace this part
if (event.isDataFlavorSupported(dataFlavor)) {
    event.acceptDrop(DnDConstants.ACTION_MOVE);
    this.panel.add(an);
    event.dropComplete(true);
    this.panel.validate();
    this.panel.repaint();
    return;
}

with this
if (event.isDataFlavorSupported(dataFlavor)) {
    event.acceptDrop(DnDConstants.ACTION_MOVE);
    panel.add(an);
    panel.revalidate();
    event.dropComplete(true);
    return;
}

