如何从 Swing 应用程序拖放到 Windows 资源管理器?

Posted

技术标签:

【中文标题】如何从 Swing 应用程序拖放到 Windows 资源管理器?【英文标题】:How do I drag and drop from a Swing application to a windows explorer? 【发布时间】:2020-10-29 01:37:48 【问题描述】:

我想将 Swing 组件拖放到 Windows 资源管理器中。

我发现这个答案与我需要的相反,尽管标题相反: Swing application -> Drag & drop to the desktop / folder

还发现提到 OLE 接口和 JNI 接口的答案: https://coderanch.com/t/659365/java/Drag-Drop-Explorer

还有这个线程,其中 OP 与我有完全相同的问题: https://www.dreamincode.net/forums/topic/148892-drag-and-drop-to-windows-explorer/

【问题讨论】:

在您的最后一个链接中,他们发布了一个指向 BasicDnD Java 类的链接,如果您复制粘贴、编译并运行该类,并在程序的左下角启用拖放功能,则将获得一个工作示例,它可以双向处理拖放,从 Swing 到另一个程序,反之亦然。如果这不起作用,请发布正确的minimal reproducible example 来证明您的问题。 【参考方案1】:

这是一个示例,涉及将文件从 JTree 拖放到 Windows 资源管理器:

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class FileBrowser implements Runnable 

    private DefaultMutableTreeNode root;

    private DefaultTreeModel treeModel;

    private JTree tree;

    @Override
    public void run() 
        JFrame frame = new JFrame("File Browser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        File fileRoot = File.listRoots()[0];
        root = new DefaultMutableTreeNode(new FileNode(fileRoot));
        treeModel = new DefaultTreeModel(root);

        tree = new JTree(treeModel);
        tree.setShowsRootHandles(true);
        tree.setDragEnabled(true);
        tree.setTransferHandler(new FileTransferHandler());
        JScrollPane scrollPane = new JScrollPane(tree);

        frame.add(scrollPane);
        frame.setLocationByPlatform(true);
        frame.setSize(640, 480);
        frame.setVisible(true);

        CreateChildNodes ccn = new CreateChildNodes(fileRoot, root);
        new Thread(ccn).start();
    

    public class CreateChildNodes implements Runnable 

        private DefaultMutableTreeNode root;

        private File fileRoot;

        public CreateChildNodes(File fileRoot, DefaultMutableTreeNode root) 
            this.fileRoot = fileRoot;
            this.root = root;
        

        @Override
        public void run() 
            createChildren(fileRoot, root);
        

        private void createChildren(File fileRoot, DefaultMutableTreeNode node) 
            File[] files = fileRoot.listFiles();
            if (files == null)
                return;

            for (File file : files) 
                DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(new FileNode(file));
                node.add(childNode);
                if (file.isDirectory()) 
                    createChildren(file, childNode);
                
            
        

    

    public class FileNode 

        private File file;

        public FileNode(File file) 
            this.file = file;
        

        @Override
        public String toString() 
            String name = file.getName();
            if (name.equals("")) 
                return file.getAbsolutePath();
             else 
                return name;
            
        
    

    private class FileTransferHandler extends TransferHandler 

        private static final long serialVersionUID = 1L;

        @Override
        protected Transferable createTransferable(JComponent c) 
            JTree list = (JTree) c;
            List<File> files = new ArrayList<File>();
            for (TreePath path : list.getSelectionPaths()) 
                files.add(new File(Arrays.stream(path.getPath()).map(Object::toString).collect(Collectors.joining(File.separator))));
            
            return new FileTransferable(files);
        

        @Override
        public int getSourceActions(JComponent c) 
            return MOVE;
        
    

    private class FileTransferable implements Transferable 

        private List<File> files;

        public FileTransferable(List<File> files) 
            this.files = files;
        

        public DataFlavor[] getTransferDataFlavors() 
            return new DataFlavor[]  DataFlavor.javaFileListFlavor ;
        

        public boolean isDataFlavorSupported(DataFlavor flavor) 
            return flavor.equals(DataFlavor.javaFileListFlavor);
        

        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException 
            if (!isDataFlavorSupported(flavor)) 
                throw new UnsupportedFlavorException(flavor);
            
            return files;
        
    

    public static void main(String[] args) 
        SwingUtilities.invokeLater(new FileBrowser());
    


【讨论】:

以上是关于如何从 Swing 应用程序拖放到 Windows 资源管理器?的主要内容,如果未能解决你的问题,请参考以下文章

Swing 应用程序 -> 拖放到桌面/文件夹

自定义对象从 FX 拖放到 Swing

将文件从操作系统拖放到 Java 应用程序 (Swing)

将文件路径拖放到 Java Swing JTextField

如何允许从 Windows 资源管理器拖放到 C# WPF 应用程序中?

将自定义对象从 JList 拖放到 JLabel