Learn how to a build a cloud-first strategyRegister Now


Remote File Drag And Drop To Local PC

Posted on 2004-11-18
Medium Priority
Last Modified: 2010-05-18

I am trying to create a Remote File Drag And Drop Dialog to allow a user to drag a file from a Jlist that shows files
from a specified directory on a server.  I have found some code to get to that point.  My JList is a custom JList called
Droppable List that implements DropTargetListener, DragSourceListener, DragGestureListener.

In the implemented dragGestureRecognized method, I create a FileSelection object.  Then, I call the startDrag method()
as shown. :

FileSelection transferable = new FileSelection(fileNameDesignator);

The FileSelection object implements the Transferrable interface.  FileSelection extends ArrayList and adds the argument fileNameDesignator (a String) to the FileSelection object.  

Now, during the drag operation, I see the FileSelection getTransferData() get called.

In the getTransferData() method, I retrieve the fileNameDesignator string that I sent in using  the code:(String)get(0).
Now, another piece of this open source code that is part of this project knows how to take this fileNameDesignator string
and retrieve the data as a large String from the remote file.

I then use this code (after I get the fileNameDesignator string).  This code creates a temporary file from the data that
I have retrieved from the server.  So, now my data goes into temp.dat and this works.  The problem is that the return
of the getTransferData() method is causing a Class Cast Exception.  I am not sure what the Class is supposed to be that
getTransferData() is returning.  What would that be?

FileOutputStream outfile = new FileOutputStream("c:\\temp.dat");
DataOutputStream outData = new DataOutputStream(outfile);
return ("c:\temp.dat");  <-- Class cast exception thrown from here.  I tried returning a File object and that causes the same exception.

Anyway, the end result that I want is to create a file with the same name by dragging it to a Windows Explorer window. How does Java know what File object (including the complete path) to create.  I've looked at local file drag drop examples and I can't find where this occurs.

Do you have any suggestions for me in helping me get my code to work?  I know I may have some issues in several areas
here so any comments about anything would be greatly appreciated.  I am a novice to the Drag and Drop interface although
I have worked with Java since 1998 in other APIs.



Question by:danw11
  • 5
LVL 86

Expert Comment

ID: 12619498
>>The problem is that the return
of the getTransferData() method is causing a Class Cast Exception.  I am not sure what the Class is supposed to be that
getTransferData() is returning.  What would that be?

You can find out

LVL 86

Expert Comment

ID: 12619506
return ("c:\temp.dat");  

should be

return ("c:\\temp.dat");  

or better

return ("c:/temp.dat");  
LVL 86

Expert Comment

ID: 12619526
Ignore my first comment !
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 12619640
FileSelection should implement Transferable and getTransferData(DataFlavor flavor) should return a List of File when DataFlavor.javaFileListFlavor is requested, otherwise throwing an exception for an unsupported flavor

Author Comment

ID: 12628350
Thanks CEHJ.  That's all that was missing. Once the getTransferData returned the correct type.  Things worked as expected.  There is some logic as part of the API that KNOWS how to create a file in the dragged-to-file directory on the Local PC.


LVL 86

Expert Comment

ID: 12628819

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question