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]
# 1
I have a bad feeling that you want to do something impossible. Could you maybe describe a little more what you are trying to do? Especially this part "Desktop somehow magically returns the path where it was dropped"
cotton.ma at 2007-7-12 9:20:03 > top of Java-index,Java Essentials,Java Programming...
# 2

I'm sorry for my vague description. What I mean is the following. WinRAR and WinZip for example, have this feature to drag a file from its list, and drop it in a folder. Once you have done that, it is extracted from the archive, and placed inside the folder. 7Zip, ArchiveManager(on my Ubuntu box) also have these features.

I figured I would need a path where it was dropped in order to place my entry there accordingly, though, I think I'm a bit far off at the moment.

Thanks

Leviaa at 2007-7-12 9:20:03 > top of Java-index,Java Essentials,Java Programming...
# 3
you need to implement:DropTargetListener,DragSourceListener,DragGestureListenerinterfaces and implement methods to handle drag sourceing and dropping functionality
grilleda at 2007-7-12 9:20:03 > top of Java-index,Java Essentials,Java Programming...
# 4

Ok, thank you for your reply. I have gone through the following article:

http://www.codeproject.com/java/dnd.asp

and it seems to do exactly what I want, though I don't actually know where the magic happens, when you drag a item from the listbox to a folder, the file on the computer is moved to that folder. If I'd just would be able to change that behavior...

Ill have a look at that tutorial, implement it in my table, and see further. Thanks

Leviaa at 2007-7-12 9:20:03 > top of Java-index,Java Essentials,Java Programming...
# 5

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?

Leviaa at 2007-7-12 9:20:03 > top of Java-index,Java Essentials,Java Programming...