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);
}
});
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.
精彩评论