开发者

Drag & drop with JLabel

开发者 https://www.devze.com 2022-12-12 09:54 出处:网络
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.

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);
    }
});


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号