Drag and Drop

Drag and Drop

Posted in : Core Java Posted on : October 29, 2010 at 12:06 PM Comments : [ 0 ]

This section contains the detail about the Drag and Drop in java.

Drag and Drop

In GUI, the drag and drop is the action of selecting an object and dragging it to another location using Mouse. Usually, we can drag and drop two things. Data or some graphical objects. If we drag an image from one application to another, we drag and drop binary data. If we drag a tab in Firefox and move it to another place, we drag and drop a graphical component.

In Swing, many components have already a built-in support for drag and drop operations. In such cases, a Swing programmer uses a TransferHandler to manage the drag and drop functionality. In situations, where there is no built-in support, the programmer has to create everything from scratch.

Example :

Given below the simple drag and drop example . We are utilizing a TransferHandler class because we have built-in drag and drop support :

package corejava;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.TransferHandler;

public class DragNDrop extends JFrame {
	JTextField field;
	JButton button;

	public DragNDrop() {

		setTitle("Simple Drag & Drop");


		button = new JButton("Button");
		button.setBounds(200, 50, 90, 25);

		field = new JTextField();
		field.setBounds(30, 50, 150, 25);


		button.setTransferHandler(new TransferHandler("text"));

		setSize(330, 150);

	public static void main(String[] args) {
		new DragNDrop();

Output :

After executing program & selecting typed text :

When we drag the typed text on to button :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics