Require help to implement drag from Java Application and drop to Native File System
I have a Java Swing application which show开发者_JAVA百科s a list of the files/Folders that have been uploaded to the server. I have a requirement to download selected file(s)/folder(s) to native file system using Drag and Drop . I have searched links but almost every link describes the Drag and Drop within a Java Swing application. My requirement is to Drag the file from the Java Swing application and Drop it to the native file system.
I need help to know how I can get the location where user has dropped the selected file(s)/folder(s) to the native file system.
For example, let's suppose the user has dragged the file and dropped directly to the C:\Back_Up
folder by restore the window of Java Application. How can I identify the location that user has dropped the file to, i.e. C:\Back_Up
?
AFAIK, it's not possible to get the information, where a dragged object has been dropped. Guess, your idea was to know the drop point and copy the file to that position in a second step.
But I think, it doesn't work this way and, even worse, the whole think could be pretty OS dependant. I bet, you have to put the entire file on the transfer. I know that it's possible with SWT, but SWT ships with some native libraries...
Here's at least one link that shows an example for the other way round: Drag and drop of a group of files into a tree
Thanks Andreas.. We have a JAVA component as a Table from which we drag the file and dropped to native file system. We have code like
A> Component is JXTree. We have set following property to support Drag And Drop.
Component.setDropMode(DropMode.INSERT);
Component.setDragEnabled(true);
DragSource ds = DragSource.getDefaultDragSource();
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer( Component,
DnDConstants.ACTION_MOVE, new FileDragGestureListener());
B> We have written a class which implements Drag Gesture Listener.
public class FileDragGestureListener extends DragSourceAdapter implements DragGestureListener {
public void dragGestureRecognized(DragGestureEvent dge) {
We get selected row from table.
Download the selected File to Native file System's TEMP directory.
FileSystemView fsv = FileSystemView.getFileSystemView();
Icon icn = fsv.getSystemIcon(File);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getBestCursorSize(icn.getIconWidth(), icn.getIconHeight());
BufferedImage buff = new BufferedImage(dim.width, dim.height,
BufferedImage.TYPE_INT_ARGB);
if (DragSource.isDragImageSupported()) {
evt.startDrag(DragSource.DefaultCopyDrop, buff, new Point(0, 0),
new TextFileTransferable(File),
this);
} else {
cursor = tk.createCustomCursor(buff, new Point(0, 0), "sString");
evt.startDrag(cursor, null, new Point(0, 0),
new TextFileTransferable(File),
this);
}
}
class TextFileTransferable implements Transferable {
File temp;
public TextFileTransferable(File temp) throws IOException {
this.temp = temp;
}
public Object getTransferData(DataFlavor flavor) {
List list = new ArrayList();
list.add(temp);
return list;
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] df = new DataFlavor[1];
df[0] = DataFlavor.javaFileListFlavor;
return df;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (flavor == DataFlavor.javaFileListFlavor) {
return true;
}
return false;
}
}
So this is how we can able to download the file up to %TEMP% then we can not move that file to a location where is has been dropped.
Please suggest where i am wrong OR what best approach to implement this Drag And Drop.
Thanks
I believe Java Desktop API could accomplish this. Not sure about details, though...
Maybe if you knew in general what directories people would be dropping into, your application could create file objects pointed at those directories and then use the Observer design pattern to tell your application when something got dropped into them (by polling the contents, maybe) although if other programs/people dropped things into those directories you'd get notified of that too, but then you could compare the time that notification came in with the time you could know that you did some dropping. This would reduce it to the arguably low probability (yet uncomfortably still possible) case of someone else dropping into directory A at almost the exact same time you dropped into directory B.
Good luck!
Some clue can be obtained from JDesktop Integration Components: https://jdic.dev.java.net/
精彩评论