Java Tutorial – Swing Text Drag And Drop – Sample Example

In computer graphical user interfaces, drag-and-drop or DnD is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects. As a feature, support for drag-and-drop is not found in all software, though it is sometimes a fast and easy-to-learn technique for users to perform tasks. However, the lack of affordances in drag-and-drop implementations means that it is not always obvious that an item can be dragged.

In this tutorial, I will demonstrate the ability to do a drag and drop between a text field and a text area or vice versa in Java Swing GUI development. I will also show how dragging of a text from a text area or text field can be disabled, preventing the text from that field from getting dragged and drop. This is a very powerful feature of Java, and thanks to those who thought of simplifying developer’s life by implementing the feature in API. All we have to do now is to sit back and use the feature which has already been developed.

package com.kushal.gui;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On 07-14-2009
 *
 * This class demonstrates the ability to do
 * drag and drop of selected text between a text field
 * and a text area.
 *
 */
public class DragAndDropDemo extends JFrame {
	private static final long serialVersionUID = 1L;

	public DragAndDropDemo()
	{
		super("www.sanjaal.com/java - Drag and Drop Demo");

		Box verticalBox = Box.createVerticalBox();
		/**
		 * Defining a text field.
		 * Setting setDragEnabled() to true.
		 * You can drag on this field, or drag text from
		 * this field to any other field.
		 */
		JTextField textField = new JTextField(15);
		textField.setText("Select me and drag me to other fields.");
		textField.setDragEnabled(true);
		verticalBox.add(textField);

		/**
		 * Defining a text area.
		 * Setting setDragEnabled() to true.
		 * You can drag on this field, or drag text from
		 * this field to any other field.
		 */

		JTextArea textArea = new JTextArea(4, 25);
		textArea.setText("www.sanjaal.com/java is great.");
		textArea.setDragEnabled(true);
		verticalBox.add(new JScrollPane(textArea));

		/**
		 * Adding a text field by setting setDragEnabled() to false.
		 * You can drag over this text field, but cannot drag
		 * the text on this field to any other fields.
		 */
		JTextField textField1 = new JTextField(15);
		textField1.setText("You cannot drag me.");
		textField1.setDragEnabled(false);
		verticalBox.add(textField1);

		JPanel panel=new JPanel();
		panel.add(verticalBox);
		getContentPane().add(panel);

		/**
		 * Setting some common GUI Features
		 */
		pack();
		setVisible(true);
		setLocation(200,200);
		setSize(400,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	/**
	 * Testing the DragAndDrop GUI program.
	 */
	public static void main(String[] args) {
		new DragAndDropDemo();

	}
}

—————————-
Here is the output:

[My Video Promotion]
  1. Java Object Serialization and Deserialization In MySQL Database
  2. How To Set Conditional Debug Breakpoints in Eclipse or IBM RAD?
  3. Rotating An Image In Java Graphics 2D By Specified Degree
  4. Java – How To Overlay One Image Over Another Using Graphics2D [Tutorial]
  5. Quick SQL Reference – Find Duplicate Data In A Table Using Having Clause
  6. Splitting PDF File Using Java iText API Into Multiple PDFs
  7. Object Serialization And De-Serialization In Java To Filesystem
  8. How to display a swing window (GUI) or JFrame always on top?
  9. Finding Java Image Pixels Information – ARGB (Alpha, Red, Green Blue)
  10. Creating A 3D Pie Chart In Java Using JFreeChart [Example Source Code]
  11. Java Tutorial – Swing Text Drag And Drop – Sample Example
  12. Serialzing A Java Object Into XML and De-Serialzing using XMLEncoder And XMLDecoder
  13. Six Stages Of Debugging in Software Engineering
  14. Java 2D Graphics Example Tutorial On Paint, Gradient and Stroke
  15. Complete Tutorial On Using SOAP-UI to Mock Web Service Request / Response
  16. Listing The Content Of Zip File With Zip Information In Java
  17. Joke – Why do Java Programmers wear glasses?
  18. How to receive files via bluetooth in your MacBook Pro
  19. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

6 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

  5. Neethu says:

    Thank you very much for the solution. This is the one which i was searching.

  6. Manikant says:

    I need to copy to the shared folder which is shared and can be authenticate by using credentials.

    but here the code is giving to write .

    I need copy my folder from my local to shared location

    Please help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.