How to override JLabel importData

Go To StackoverFlow.com

1

I am trying a very simple override. I just want to add one line of code to importData for a 'text' TransferHandler. My problem is that I can't find the code I need to copy into my override method before my one line of code!

public class JLabelTransferHandler extends TransferHandler
{
    private Logger logger;  // My error logging class

    public JLabelTransferHandler()
    {
        super("text");
        logger = LoggerFactory.getLogger(this.getClass());
    }

    public boolean importData(TransferSupport support) 
    {
        // WHAT GOES HERE?????
        logger.info("Data imported");           
        return true;
    }
}
2012-04-04 23:05
by Lou Morda
Have you looked at the Swing tutorial on this? Specifically this link - Hovercraft Full Of Eels 2012-04-04 23:36


4

Per the tutorial, looks like you should first check to see if your component supports transfer of this flavor type, and so you'd call the canImport(...) method (one that you'll likely need to override), and if so, extract the Transferable from the TransferSupport parameter, get its String data and then put it into your JLabel. Simple. Again, the link above shows all.

2012-04-04 23:46
by Hovercraft Full Of Eels
Unfortunately I kept googling "JLabel drag and drop", and that tutorial wasn't coming up. Argh - Lou Morda 2012-04-05 00:39
You were Googling too specific a question. You just need to Google Java drag and drop, and then extend what you find to JLabels - Hovercraft Full Of Eels 2012-04-05 00:41
The problem I was having is that I didn't know how to put the String data into my JLabel... can I just do a jLabelText.setText(data); where 'data' is the String data extracted from the Transferrable? All the examples use JLists and JTrees, and have these complicated 'insertAt' methods with drop locations. It seems strange using the setText method inside of the TransferHandler - Lou Morda 2012-04-05 00:54
@Louis: better than asking here -- try it! You might be pleasantly surprised (as I was when I tried it about an hour ago) - Hovercraft Full Of Eels 2012-04-05 00:55
Well, it worked. So let's just stick with that. Thanks!!! : - Lou Morda 2012-04-05 00:56
Ads