I have written an program where I need to be able to drag my own component (extends JPanel implements Transferable) either on to another of this component or to an external application eg.MS Word, Powerpoint etc.

When dragging onto another of my component I want it to pass a String and when into another application I want it to transfer a BufferedImage. I have written a simpler version and included it below and the component in question will be the MyPanel components. At the moment my program always transfers the String reference but when dragging into MS Word I want it to transfer the BufferedImage image. Any ideas as to where I have gone wrong? I found out that if I remove DataFlavor.stringFlavor from the array it will transfer the image but then ofc it no longer transfers reference to other MyPanel objects.

Thank you in advance.

Java Code:
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;

public class DragTest {

	public static void main(String args[]) {

		new DragTest();

	public DragTest() {

		JFrame frame = new JFrame("Drag test");
		frame.setMinimumSize(new Dimension(250, 150));

		frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
		frame.getContentPane().add(new MyPanel("Left"));
		frame.getContentPane().add(new MyPanel("Right"));


Java Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceContext;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.dnd.InvalidDnDOperationException;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JPanel;

public class MyPanel extends JPanel implements Transferable {

	private String reference;
	private BufferedImage image;
	private DataFlavor dataFlavours[] = new DataFlavor[] { DataFlavor.imageFlavor,
			DataFlavor.stringFlavor };

	public MyPanel(String aReference) {

		reference = aReference;
		this.setSize(100, 100);

	 * Paint my own image which in my real application would be a graph. White background with a blue
	 * diagonal line
	private void paintMyImage() {

		image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = (Graphics2D) image.createGraphics();
		g2.fillRect(0, 0, this.getWidth(), this.getHeight());
		g2.drawLine(0, this.getHeight(), this.getWidth(), 0);

	public void paintComponent(Graphics g) {

		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(image, null, 0, 0);

	 * From implementing Transferable
	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {

		if (flavor.equals(DataFlavor.imageFlavor)) {
			return image;
		// else if (flavor.equals(DataFlavor.stringFlavor)) {
		if (flavor.equals(DataFlavor.stringFlavor)) {
			return new String(reference);
		return null;

	 * From implementing Transferable
	public DataFlavor[] getTransferDataFlavors() {

		return dataFlavours;

	 * From implementing Transferable
	public boolean isDataFlavorSupported(DataFlavor flavor) {

		for (DataFlavor loopFlavour : dataFlavours) {

			if (loopFlavour.equals(flavor)) {
				return true;
		return false;

	 * create drag and drop functionality
	private void setDragAndDrop() {

		DragSource dragSource;
		DropTarget dropTarget;
		DragGestureListener dgListener;
		DropTargetListener dtListener;
		final DragSourceListener dsListener;
		final MyPanel panel = this;

		dragSource = DragSource.getDefaultDragSource();

		dsListener = new DragSourceListener() {

			public void dropActionChanged(DragSourceDragEvent dsde) {


			public void dragOver(DragSourceDragEvent dsde) {


			public void dragExit(DragSourceEvent dse) {


			public void dragEnter(DragSourceDragEvent dsde) {

				System.out.println("Drag enter");
				DragSourceContext context = dsde.getDragSourceContext();
				// intersection of the users selected action, and the source and target actions
				int myaction = dsde.getDropAction();
				if ((myaction & DnDConstants.ACTION_MOVE) != 0) {
				} else {


			public void dragDropEnd(DragSourceDropEvent dsde) {

				if (dsde.getDropSuccess() == false) {
				int dropAction = dsde.getDropAction();
				if (dropAction == DnDConstants.ACTION_MOVE) {


		dgListener = new DragGestureListener() {

			public void dragGestureRecognized(DragGestureEvent dge) {

				try {
					Transferable trans = panel;

					dge.startDrag(DragSource.DefaultCopyDrop, trans, dsListener);

				} catch (InvalidDnDOperationException e) {


		dtListener = new DropTargetListener() {

			public void dropActionChanged(DropTargetDragEvent dtde) {


			public void drop(DropTargetDropEvent dtde) {

				System.out.println("drop: " + reference);

			public void dragOver(DropTargetDragEvent dtde) {


			public void dragExit(DropTargetEvent dte) {


			public void dragEnter(DropTargetDragEvent dtde) {

				System.out.println("drag enter: " + reference);

		// component, action, listener
		dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE,
		dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, dtListener);