Making a component drag gable in java

In this section, you will learn how to make a component draggable in java.

Making a component drag gable in java

In this section, you will learn how to make a component draggable in java.

Making a component drag gable in java

Making a Component Draggable in Java

     

In this section, you will learn how to make a component draggable in java. This program provides the drag and drop feature from one component to another. This program shows a text box and a label on the frame. When you drag the label and drop to the text box then the text of the label is copied to the text box. For the pictorial representation of the application result is given as follows:

Drag And Drop

Code Description:

There are several APIs has been used to make the component draggable in the program. These are explained as follows:

TransferHandler:
This is the class of the javax.swing.*; package. This is used to transfer the transferable swing component from on the another. Both components should be the swing component. This class is helpful to copy from one component to another via clipboard.

setTransferHandler():
This is the method of the TransferHandler class which makes the component able to transfer the specified part of the component in the
TransferHandler() as parameter which specifies the the property of the component which has to be dragged and dropped. This program specifies the text of the label to be dragged and drop to the text box using the string "text".

MouseListener:
This is the interface which receives the different mouse events. Mouse events can be like: pressing or releasing the mouse, click, enter or exit the mouse etc. are handled through the MouseEvent generated by the MouseListener interface. This program uses the instance of the MouseListener.

exportAsDrag():
This is the method of the TransferHandler class which initiates the component to drag and drop from one to another swing component. This method takes three arguments like: JComponent, Active event, and another is the Action which has to be done. Here, the constant property COPY of the TransferHandler class has been used to copy the text from the label to the text box.

Here is the code of the program: 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingDragDrop{
  JTextField txtField;
  JLabel lbl;
  public static void main(String[] args){
  SwingDragDrop sdd = new SwingDragDrop();
  }

  public SwingDragDrop(){
  JFrame frame = new JFrame("Drag Drop Demo");
  txtField = new JTextField(20);
  lbl = new JLabel("This is the text for drag and drop.");
  lbl.setTransferHandler(new TransferHandler("text"));
  MouseListener ml = new MouseAdapter(){
  public void mousePressed(MouseEvent e){
  JComponent jc = (JComponent)e.getSource();
  TransferHandler th = jc.getTransferHandler();
  th.exportAsDrag(jc, e, TransferHandler.COPY);
  }
  };
  lbl.addMouseListener(ml);
  JPanel panel = new JPanel();
  panel.add(txtField);
  frame.add(lbl, BorderLayout.CENTER);
  frame.add(panel, BorderLayout.NORTH);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  }
}

Download this example.