Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:46:53+00:00 2026-06-12T22:46:53+00:00

I’m basically trying to drag and drop a JPanel which contains a Jlabel: Here

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:46:54+00:00Added an answer on June 12, 2026 at 10:46 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.