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)