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:

Serialzing A Java Object Into XML and De-Serialzing using XMLEncoder And XMLDecoder

This is my third post related to Java Object Serialization.

In the first post, I talked about serializing a Java Object to a binary file system.

I dealt with serializing the object to MySQL database in my second post.

In this post, I am providing you a working code on how to write (serialize) a java object into XML file using java.beans.XMLEncoder and reading the XML File into Java Object using java.beans.XMLDecoder

The XMLDecoder class is used to read XML documents created using the XMLEncoder and is used just like the ObjectInputStream. For example, one can use the following fragment to read the first object defined in an XML document written by the XMLEncoder class.

The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects. Despite the similarity of their APIs, the XMLEncoder class is exclusively designed for the purpose of archiving graphs of JavaBeans as textual representations of their public properties. Like Java source files, documents written this way have a natural immunity to changes in the implementations of the classes involved. The ObjectOutputStream continues to be recommended for interprocess communication and general purpose serialization.

package com.kushal.serialization;

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On: 10th September 2009
 *
 * Serializing Object To XML
 * Deserializing Object From XML
 */

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ObjectSerializationToXML {

	/**
	 * This method saves (serializes) any java bean object into xml file
	 */
	public void serializeObjectToXML(String xmlFileLocation,
			Object objectToSerialize) throws Exception {
		FileOutputStream os = new FileOutputStream(xmlFileLocation);
		XMLEncoder encoder = new XMLEncoder(os);
		encoder.writeObject(objectToSerialize);
		encoder.close();
	}

	/**
	 * Reads Java Bean Object From XML File
	 */
	public Object deserializeXMLToObject(String xmlFileLocation)
			throws Exception {
		FileInputStream os = new FileInputStream(xmlFileLocation);
		XMLDecoder decoder = new XMLDecoder(os);
		Object deSerializedObject = decoder.readObject();
		decoder.close();

		return deSerializedObject;
	}

	/**
	 * Testing.
	 * 1. Creates and Object.
	 * 2. Serializes Object To XML
	 * 3. Deserializes Object From XML
	 * 4. Prints The values hold in Object
	 */
	public static void main(String args[]) throws Exception {

		/* Location of XML File */
		String XMLLocation = "C:/myXMLFile.xml";

		ObjectSerializationToXML serializer = new ObjectSerializationToXML();

		/* Creating and filling a bean object */
		MyBeanToSerialize obj = new MyBeanToSerialize();
		obj.setFirstName("Johnny");
		obj.setLastName("Depp");
		obj.setAge(45);

		/* Serialzing Object to XML */
		System.out.println("Starting Serialization...");
		serializer.serializeObjectToXML(XMLLocation, obj);
		System.out.println("Serialized Object: " + obj.getClass().getName());
		System.out.println("Destination XML: " + XMLLocation);

		/* Reading the object from serialized XML */
		System.out.println("nnStarting De-Serialization...");
		System.out.println("Source XML: " + XMLLocation);
		MyBeanToSerialize deserializedObj = (MyBeanToSerialize) serializer
				.deserializeXMLToObject(XMLLocation);
		System.out.println("De-serialized Object: "
				+ deserializedObj.getClass().getName());
		System.out.println("nChecking For Values In De-Serialized Object");
		System.out.println("...First Name: " + deserializedObj.getFirstName());
		System.out.println("...Last Name: " + deserializedObj.getLastName());
		System.out.println("...Age: " + deserializedObj.getAge());

	}

}

The following is the java bean that will be serialized / deserialized.

package com.kushal.serialization;
/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On: 10th September 2009
 *
 * Simple Java Bean
 */
public class MyBeanToSerialize {
	private String firstName;
	private String lastName;
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}

================================
Output of running the file ObjectSerializationToXML
Starting Serialization…
Serialized Object: com.kushal.serialization.MyBeanToSerialize
Destination XML: C:/myXMLFile.xml

Starting De-Serialization…
Source XML: C:/myXMLFile.xml
De-serialized Object: com.kushal.serialization.MyBeanToSerialize

Checking For Values In De-Serialized Object
…First Name: Johnny
…Last Name: Depp
…Age: 45

The content of the generated XML File:

Six Stages Of Debugging in Software Engineering

I came across these six stages of debugging somewhere over the internet. So, I created a poster for you guys to print and hang on to your cubes or workplace, if you want. I don’t know the original author of this six stages – so I don’t know who to attribute the original work to. But these stags are great realities of probably anyone’s debugging life.

Here is the text version of these six stages of software debugging

  1. That can’t happen
  2. That doesn’t happen on my machine.
  3. That shouldn’t happen.
  4. Why does that happen?
  5. Oh, I see.
  6. How did that ever work?