Phaelax 52 Practically a Posting Shark

The object I use to create a Transferable object with is the same object I should get on the drop component, right?

All I want to do is drag a row from one JTable onto another JTable. I don't want to copy the data, only send index number of the dragged row. I've hard-coded "5" into the Transferable object for now.

The dragGestureRecognized() method in the DragGestureListener doesn't seem to ever get called. I had it output to the terminal whenever it was called, but it never showed any output. But wouldn't it have to have been called for any of the remaining DnD features to work? When I output the data from the Transferable object in the drop() method, it displays my table data as an HTML table structure; it also shows several supported DataFlavors even though the Transferable that it was supposedly created with supports only 1. It doesn't seem to be using my method to initiate the drag.


Here's the drag code:

class DSListener extends DragSourceAdapter
		{
			public void dragEnter(DragSourceDragEvent e)
			{
				DragSourceContext context = e.getDragSourceContext();
				int action = e.getDropAction();
				if ((action & DnDConstants.ACTION_COPY) != 0)
					context.setCursor(DragSource.DefaultCopyDrop);
				else
					context.setCursor(DragSource.DefaultCopyNoDrop);
			}
			
			public void dragDropEnd(DragSourceDropEvent e)
			{
				if (e.getDropSuccess() == false)
					return;
				
				int dropAction = e.getDropAction();
				if (dropAction == DnDConstants.ACTION_COPY)
				{
				}
			}

		}
		
		final DragSource dragSource = DragSource.getDefaultDragSource();
		final DragSourceListener dsListener = new DSListener();
				
		class DGListener implements DragGestureListener
		{
			public void dragGestureRecognized(DragGestureEvent e)
			{
				Transferable transferable = new TableTransferable(5);
				e.startDrag(DragSource.DefaultCopyNoDrop, transferable, dsListener);
			}
		}
		
		DragGestureListener dgListener = new DGListener();
		 //viewTable is a JTable
		dragSource.createDefaultDragGestureRecognizer(viewTable, DnDConstants.ACTION_COPY, dgListener);

And here's the drop code:

class DTListener extends DropTargetAdapter
		{
			public void drop(DropTargetDropEvent e) 
			{
				//purposely left out validation for the moment

				DataFlavor[] flavors = e.getTransferable().getTransferDataFlavors();
				DataFlavor flavor = flavors[0];
				e.acceptDrop(DnDConstants.ACTION_COPY);
				Object obj = null;
				try
				{
					obj = e.getTransferable().getTransferData(flavor);
				}
				catch(Throwable t)
				{
					System.out.println(t);
				}
				System.out.println(obj);
				if (obj == null)
				{
					e.dropComplete(false);
					return;
				}
				
				if (obj instanceof Integer)
				{
					System.out.println(obj);
					e.dropComplete(true);
				}
				
			}
		}
		
		DropTargetListener dtListener = new DTListener();
		DropTarget dropTarget = new DropTarget(sourceTable, DnDConstants.ACTION_COPY, dtListener);

Here's the Transferable class if it helps:

import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;

/**
 * Write a description of class TableTransferable here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TableTransferable implements Transferable
{
	private Integer index;
	private static DataFlavor[] supportedFlavors = {new DataFlavor(new Integer(0).getClass(), "Integer-object")};
	
	/**
	 * Constructor for objects of class TableTransferable
	 */
	public TableTransferable(Integer i)
	{
		index = i;
	}
	
	public TableTransferable(int i)
	{
		this(new Integer(i));
	}
	
	public Object getTransferData(DataFlavor flavor)
	{
		return index;
	}

	public DataFlavor[] getTransferDataFlavors()
	{
		return supportedFlavors;
	}
	
	public static DataFlavor[] getSupportedDataFlavors()
	{
		return supportedFlavors;
	}
	
	public boolean isDataFlavorSupported(DataFlavor flavor)
	{
		for(int i=0; i<supportedFlavors.length; i++)
		{
			if (flavor.getHumanPresentableName().equals(supportedFlavors[i].getHumanPresentableName()))
				return true;
		}
		return false;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.