1

I have an application that converts a LaTeX formula and put it in a JLabel Icon. Now I want to drag & drop this Icon outside the application for example to some directory (that is creating an image file) or to some text (mail, document, etc.)

Googling it, I have only found methods to drag the Icon to another Component of my application or to drag text outside my application but neither combining this methods I got my goal..

EDIT - 25/05/2013

For simplicity let's say I have a text Label and I want to drag and drop the text of the Label outside my application (I think that if I can do this job then doing the same with an Icon Label will not be so difficult):

Jlabel label = new JLabel("Text");
label.setTransferHandler(new TextTransferHandler());

I took the advice of Guillaume Polet and I wrote the following class:

public class TextTransferHandler extends TransferHandler {

    @Override
    public int getSourceActions(JComponent c) {
        return COPY;
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
        JLabel label = (JLabel)c;
        return new StringSelection(label.getText());
    }

}

But when I try to drag the text on the Label nothing happens (neither in another component of my application nor outside the application)

Red
  • 664
  • 2
  • 10
  • 20
  • 1
    http://stackoverflow.com/questions/1204580/swing-application-drag-drop-to-the-desktop-folder ? – Dave Jarvis May 23 '13 at 22:15
  • 1
    You have to set a `TransferHandler` on your `JLabel` and override `TransferHandler.createTransferable()` to return an appropriate response (so possible flavors, and depending on the chosen flavor, return an image or text, or etc...). The second part of the link provided by @DaveJarvis provides valuable info (especially `FileTransferHandler` and `FileTransferable` – Guillaume Polet May 23 '13 at 22:24

0 Answers0