The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JLabel  [4 examples]

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

 Related Examples
e739. Creating a JLabel Component
e740. Adding an Icon to a JLabel Component
e741. Setting the Focus of a JTextField Component Using a JLabel Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.