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

Posted

技术标签:

【中文标题】Swing 应用程序 -> 拖放到桌面/文件夹【英文标题】:Swing application -> Drag & drop to the desktop / folder 【发布时间】:2010-11-15 07:56:06 【问题描述】:

Mac 的 Finder、Windows 的资源管理器将 Swing 应用程序中的特定项目拖放到桌面和文件夹时, 如何获得我丢弃的前一条路径?

我很高兴教我一个必要的课程和方法。

【问题讨论】:

请详细说明“前关”。 【参考方案1】:

这是一个小程序,但它适用于任何框架或窗口:

public class DropApplet extends Applet implements DropTargetListener 
    // . . .
    private DropTarget dropTarget;
    // . . .

    public void init() 
        if (dropTarget == null) 
            dropTarget = new DropTarget(this, this);
        
        // . . .
    

    public void dragEnter(DropTargetDragEvent arg0) 
        // nothing
    

    public void dragOver(DropTargetDragEvent arg0) 
        // nothing
    

    public void dropActionChanged(DropTargetDragEvent arg0) 
        // nothing
    

    public void dragExit(DropTargetEvent arg0) 
        // nothing
    

    public void drop(DropTargetDropEvent evt) 
        final List result = new ArrayList();
        int action = evt.getDropAction();
        evt.acceptDrop(action);
        try 
            Transferable data = evt.getTransferable();
            DataFlavor flavors[] = data.getTransferDataFlavors();
            if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) 
                List<File> list = (List<File>) data.getTransferData(
                    DataFlavor.javaFileListFlavor);
                processFiles(list);
            
         catch (UnsupportedFlavorException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
         finally 
            evt.dropComplete(true);
            repaint();
        
    

    private void processFiles(List<File> files) throws IOException 
        // . . .
    

将文件从 swing 应用程序拖到桌面稍微复杂一些,并不是所有的 swing 组件都能做到这一点。

这是一个包含文件的 JList 示例。您可以将文件从 finder/explorer 拖放到此列表中,然后将这些文件拖到另一个位置。

package dnd;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

public class DnDFrame extends javax.swing.JFrame implements DropTargetListener 

    private DefaultListModel listModel = new DefaultListModel();
    private DropTarget dropTarget;

    /** Creates new form DnDFrame */
    public DnDFrame() 
        initComponents();
        dropTarget = new DropTarget(list, this);
        list.setModel(listModel);
        list.setDragEnabled(true);
        list.setTransferHandler(new FileTransferHandler());
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() 
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Files:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(jLabel1, gridBagConstraints);

        jScrollPane1.setViewportView(list);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        pack();
    // </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList list;
    // End of variables declaration

    public void dragEnter(DropTargetDragEvent arg0) 
        // nothing
        

    public void dragOver(DropTargetDragEvent arg0) 
        // nothing
        

    public void dropActionChanged(DropTargetDragEvent arg0) 
        // nothing
        

    public void dragExit(DropTargetEvent arg0) 
        // nothing
        

    public void drop(DropTargetDropEvent evt) 
        int action = evt.getDropAction();
        evt.acceptDrop(action);
        try 
            Transferable data = evt.getTransferable();
            if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) 
                List<File> files = (List<File>) data.getTransferData(
                        DataFlavor.javaFileListFlavor);
                for (File file : files) 
                    listModel.addElement(file);
                
            
         catch (UnsupportedFlavorException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
         finally 
            evt.dropComplete(true);
        
    

    private class FileTransferHandler extends TransferHandler 

        @Override
        protected Transferable createTransferable(JComponent c) 
            JList list = (JList) c;
            List<File> files = new ArrayList<File>();
            for (Object obj: list.getSelectedValues()) 
                files.add((File)obj);
            
            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;
        
    

【讨论】:

非常感谢。我可以从 Swing App 拖动到资源管理器吗? Add 添加了一个兼具两者的示例 我现在正在研究一些 DnD 的东西。想知道您是否有任何理由使用 AWT 的东西而不是 Swing,即 TransferHandler、TransferSupport 等。

以上是关于Swing 应用程序 -> 拖放到桌面/文件夹的主要内容,如果未能解决你的问题,请参考以下文章

从 Java Swing 应用程序拖放到 Windows 资源管理器

自定义对象从 FX 拖放到 Swing

拖放到桌面图标

拖放到桌面/资源管理器

Qt 4.x:如何实现拖放到桌面或文件夹中?

将 Windows 桌面快捷方式拖放到 listView