Adding Drag-and-Drop Support to a JLabel Component

This example demonstrates how to modify a label component so that its text can be dragged and dropped to another component.
// Create a label JLabel label = new JLabel("Label Text"); // Specify that the label's text property be transferable; the value of // this property will be used in any drag-and-drop involving this label final String propertyName = "text"; label.setTransferHandler(new TransferHandler(propertyName)); // Listen for mouse clicks label.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { JComponent comp = (JComponent)evt.getSource(); TransferHandler th = comp.getTransferHandler(); // Start the drag operation th.exportAsDrag(comp, evt, TransferHandler.COPY); } });

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.