Require help to implement drag and drop from a Java application and move to the source file system

I have a Java Swing application that shows a list of files / folders that have been uploaded to the server. I have a requirement to upload selected files / folders (s) to my own file system using Drag and Drop. I searched for links, but almost every link describes drag and drop in a Java Swing application. My requirement is to drag and drop a file from a Java Swing application and move it to your own file system.

I need help to find out how I can get the place where the user dumped the selected files / folders (s) into their own file system.

For example, suppose a user drags a file and immediately falls into the C:\Back_Up , restoring the Java application window. How can I determine the location where the user dumped the file, i.e. C:\Back_Up ?

+4
source share
5 answers

AFAIK, it is not possible to obtain information in which the deleted object was deleted. I guess your idea was to find out the point of incidence and copy the file to this position in the second step.

But I think this will not work, and, even worse, all thoughts can be quite OS dependent. I bet you have to transfer the whole file to transfer. I know this is possible with SWT, but SWT comes with some native libraries ...

Here is at least one link that shows an example for another path: Drag and drop a group of files into a tree

+1
source

Thanks Andreas .. We have a JAVA component in the form of a table from which we drag and drop the file and drop it into our own file system. We have a type code

A> Component JXTree. We set the 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 wrote a class that implements List Gesture Drag Gesture.

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 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); } } 

the TextFileTransferable class 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; } 

}

Thus, we can upload the file to% TEMP%, then we will not be able to move this file to the place where it was deleted.

Please suggest where I am mistaken, or what is the best approach to implement this Drag And Drop.

thanks

+1
source

I believe the Java Desktop API could do this. Not sure about the details though ...

0
source

Perhaps if you knew which directories people would enter, your application could create the file objects specified in these directories, and then use the Observer design pattern to tell your application when something fell into them (by polling the contents, maybe), although if other programs / people dumped things into these directories, you will also receive a notification about it, but then you can compare the time at which the notification was sent, with the time when you might know that you did it. This would reduce it to the possible low probability (but still inconvenient of the still possible) of the case when someone else ran into directory A almost at the same time that you fell into directory B.

Good luck

0
source

Some help can be obtained from the JDesktop integration components: https://jdic.dev.java.net/

0
source

All Articles