Drag and Drop with JLabel

Can I drag JLabeland paste a custom object into it or use another component? But I have to use TransferHandlerwith exportAsDrag.

My code is:

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);
    }
});
+3
source share
1 answer

Depends on what you want to drag - JLabelor just 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 this model.

+2
source

Source: https://habr.com/ru/post/1724020/


All Articles