Drag and drop files
Hello,
I've been working with Swing for a while now and it seems to work out perfectly well, only there is one thing I just don't understand. I have read numerous tutorials on this subject but wasn't able to find the current situation.
For example:
I have a JTable, populated with rows and columns. I want the user to be able to drag a row, to a folder somewhere on his computer, lets say desktop. I think the following needs to happen:
User begins dragging of the selected row
User drops it on the desktop
Desktop somehow magically returns the path where it was dropped
and some function, which I define, does the rest
What do I need to do in general to get this working?
Thanks
[743 byte] By [
Leviaa] at [2007-11-27 4:13:46]

Ok, I have written the code with help of the tutorial, though, this is what I expected that would happen.
/*
* ZFileTable.java
*
* Created: 14 may 2007
* Author: Lars 'Levia' Wesselius
* Description: The list for showing the files in the opened zip archive.
*
*/
package Zipper;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class ZFileTable extends JTable
implements DropTargetListener, DragSourceListener, DragGestureListener
{
private ZControls m_Controls;
DropTarget dropTarget = new DropTarget(this, this);
DragSource dragSource = DragSource.getDefaultDragSource();
public ZFileTable(DefaultTableModel model, ZControls controls)
{
m_Controls = controls;
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
setModel(model);
}
/* Method fired when something is dropped upon this table. */
public synchronized void drop(DropTargetDropEvent dropTargetDropEvent)
{
System.out.println("drop");
try
{
Transferable transferable = dropTargetDropEvent.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
java.util.List fileList = (java.util.List)transferable.getTransferData(DataFlavor.javaFileListFlavor);
Iterator iterator = fileList.iterator();
if (fileList.size() != 1)
{
dropTargetDropEvent.rejectDrop();
return;
}
while (iterator.hasNext())
{
File file = (File)iterator.next();
m_Controls.doArchive(file.getCanonicalPath());
}
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
}
else
{
dropTargetDropEvent.rejectDrop();
}
}
catch (IOException io)
{
}
catch (UnsupportedFlavorException ufe)
{
}
}
//////////////////////////////////////////////////////////////////////////////
public void dragOver(DropTargetDragEvent dropTargetDragEvent)
{
System.out.println("DragOver");
}
public void dragExit(DropTargetEvent dropTargetEvent)
{
System.out.println("DragExit");
}
public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent)
{
System.out.println("DropActionChanged");
}
public void dragEnter(DropTargetDragEvent dropTargetDragEvent)
{
System.out.println("DragEnter");
dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
}
//////////////////////////////////////////////////////////////////////////////
public void dragExit(DragSourceEvent dragSourceEvent)
{
System.out.println("DragExit");
}
public void dropActionChanged(DragSourceDragEvent dragSourceDragEvent)
{
System.out.println("dropActionChanged");
}
public void dragOver(DragSourceDragEvent dragSourceDragEvent)
{
System.out.println("DragOver");
}
public void dragDropEnd(DragSourceDropEvent dragSourceDropEvent)
{
System.out.println("DragdropEnd");
}
public void dragEnter(DragSourceDragEvent dragSourceDragEvent)
{
System.out.println("DragEnter");
}
//////////////////////////////////////////////////////////////////////////////
public void dragGestureRecognized(DragGestureEvent dragGestureEvent)
{
System.out.println("DragGestureRecognized");
// Get the value we want to transfer
String str = (String)getValueAt(getSelectedRow(), getSelectedColumn());
// Check if the value is not null
if (str != null)
{
ZTransferable transferable = new ZTransferable(str);
dragGestureEvent.startDrag(DragSource.DefaultCopyDrop, transferable, this);
}
}
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
//////////////////////////////////////////////////////////////////////////////
public boolean isCellEditable(int rowIndex, int vColIndex)
{
return false;
}
}
It currently accepts dropping and that works fine. Only when I drag an item from the table, and drop it on the desktop, I get an error..I suppose it should be changed and handled in the transferable. When I drag it into a text editor, I get to see the string, which is right, which comes from the transferable.
/*
* ZTransferable.java
*
* Created: 14 may 2007
* Author: Lars 'Levia' Wesselius
* Description: This class is going to transfer data via DnD.
*
*/
package Zipper;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;
import javax.swing.*;
import java.io.IOException;
import java.io.File;
import java.util.Vector;
class ZTransferable extends Vector implements Transferable
{
DataFlavor flavors[] = {
DataFlavor.javaFileListFlavor,
DataFlavor.stringFlavor
};
public ZTransferable(String str)
{
addElement(str);
}
public synchronized DataFlavor[] getTransferDataFlavors()
{
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor)
{
boolean isSupported = false;
isSupported |= flavor.equals(flavors[0]);
isSupported |= flavor.equals(flavors[1]);
return isSupported;
}
public synchronized Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
if (flavor.equals(flavors[0]))
{
return this;
}
else if (flavor.equals(flavors[1]))
{
return elementAt(0);
}
else
{
throw new UnsupportedFlavorException(flavor);
}
}
}
What do I need to change?