user217333
user217333

Reputation: 81

Drag & drop with JLabel

Can I drag a JLabel and insert into it a custom object or should I use another component? But I have to use TransferHandler with exportAsDrag.

My code:

final JLabel label1 = new JLabel("Drag here");
Collection<Person> person= new ArrayList<Person>();

//Register transferhandler objects on them label1 transfer itss
//foreground coloer label2 transfer its backgroundcolor

//need here a Transferable to put the object
label1.setTransferHandler(new TransferHandler(....));

label1.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
        TransferHandler handler = label1.getTransferHandler();
        handler.exportAsDrag(label1, e, TransferHandler.COPY);
    }
});

Upvotes: 3

Views: 910

Answers (1)

Andreas Dolk
Andreas Dolk

Reputation: 114757

Depends on what you want to drag - the JLabel or just the text.

When you drag something, you create a 'model' of the dragged object, when you drop it, you usually create something new at the destination based on that model.

Upvotes: 2

Related Questions