Remote File Drag And Drop To Local PC

Posted on 2004-11-18
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
    LVL 86

    Expert Comment

    >>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

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

    should be

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

    or better

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

    Expert Comment

    Ignore my first comment !
    LVL 86

    Accepted Solution

    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

    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


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now