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 7028583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:25:46+00:00 2026-05-28T00:25:46+00:00

I am working on a application where it is possible to drop the content

  • 0

I am working on a application where it is possible to drop the content of one JLabel into the other one. I use for this the class NameSlot:

public class NameSlot extends JLabel implements DropTargetListener{

NameSlot(String name){
    super(name);
    new DropTarget(this,this);

    this.setTransferHandler(new TransferHandler("foreground"));
    MouseListener listener = new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        JLabel comp = (JLabel) me.getSource();
        TransferHandler handler = comp.getTransferHandler();
        handler.exportAsDrag(comp, me, TransferHandler.COPY);
      }
    };
    this.addMouseListener(listener);
}



@Override
public void dragEnter(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void dragExit(DropTargetEvent dte) {
    // TODO Auto-generated method stub

}


@Override
public void dragOver(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void drop(DropTargetDropEvent dtde) {

//      DropTarget dt = (DropTarget) dtde.getSource();
//      NameSlot ns  = (NameSlot) dt.getComponent();

    try {
        JLabel l  =(JLabel) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
        System.out.println("drop detected from "+l.getText()+" to "+this.getText());
    } catch (UnsupportedFlavorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


@Override
public void dropActionChanged(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}

that’s how i initiale it:

    this.setLayout(null);
    NameSlot ns = new NameSlot("test");
    ns.setLocation(20, 20);
    this.add(ns);

    NameSlot ns2 = new NameSlot("test2");
    ns2.setLocation(20, 20);
    this.add(ns2);

after trying to use dtde.getSource() and scrapped it because of coolcfans comment, I now try to get the drag source using the Transferable of the DropTargetEvent. I get the exception:

java.awt.datatransfer.UnsupportedFlavorException: Unicode String
    at javax.swing.TransferHandler$PropertyTransferable.getTransferData(Unknown Source)
    at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(Unknown Source)
    at sun.awt.datatransfer.TransferableProxy.getTransferData(Unknown Source)
    at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(Unknown Source)
    at an.judosoft.view.bracketSheets.factory.NameSlot.drop(NameSlot.java:71)

Anyone knows the way?

  • 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-05-28T00:25:46+00:00Added an answer on May 28, 2026 at 12:25 am

    The following works:

    I changed this.setTransferHandler(new TransferHandler("foreground")) to this.setTransferHandler(new TransferHandler("text"))

    and used new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String") for the DataFlavor, though it returns a String instead of a JLabel. If you want a JLabel you’ll need to change the TransferHandler.

    import java.awt.FlowLayout;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.UnsupportedFlavorException;
    import java.awt.dnd.DropTarget;
    import java.awt.dnd.DropTargetDragEvent;
    import java.awt.dnd.DropTargetDropEvent;
    import java.awt.dnd.DropTargetEvent;
    import java.awt.dnd.DropTargetListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.IOException;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.TransferHandler;
    
    public class NameSlot extends JLabel implements DropTargetListener {
        NameSlot(final String name) {
            super(name);
            new DropTarget(this, this);
            this.setTransferHandler(new TransferHandler("text"));
            final MouseListener listener = new MouseAdapter() {
                @Override
                public void mousePressed(final MouseEvent me) {
                    final JLabel comp = (JLabel) me.getSource();
                    System.out.println(comp);
    
                    final TransferHandler handler = comp.getTransferHandler();
                    handler.exportAsDrag(comp, me, TransferHandler.COPY);
                }
            };
            this.addMouseListener(listener);
        }
    
        @Override
        public void dragEnter(final DropTargetDragEvent dtde) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void dragExit(final DropTargetEvent dte) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void dragOver(final DropTargetDragEvent dtde) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void drop(final DropTargetDropEvent dtde) {
    
            // DropTarget dt = (DropTarget) dtde.getSource();
            // NameSlot ns = (NameSlot) dt.getComponent();
    
            try {
                final String s = (String) dtde.getTransferable().getTransferData(
                        new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String"));
    
                System.out.println("drop detected from " + s + " to " + this.getText());
            }
            catch (final UnsupportedFlavorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (final IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (final ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
        @Override
        public void dropActionChanged(final DropTargetDragEvent dtde) {
            // TODO Auto-generated method stub
    
        }
    
        public static void main(final String[] args) {
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setLayout(new FlowLayout());
    
            final NameSlot ns = new NameSlot("test");
            frame.add(ns);
    
            final NameSlot ns2 = new NameSlot("test2");
            frame.add(ns2);
    
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application that requires me to implement more than one
I'm creating a drag and drop enabled application and I've been trying to use
I am working on struts2 application. I have getter/setter in my action class. Now,
I am working on one application in which I have to save the data
I am working on a fairly large MVC 3 application, and I'm running into
Possible Duplicate: Speech to text API for iphone? I am working on an application
I'm absolutely stumped at this. My properly working application [on iOS<=3.1] was crashing on
I'm working on timetabling application and I have a class: called ClassOption representing a
I am working on one application in which i need to show slide show
I am working on splitting out an existing, working application that I currently have

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.