拖拽文件获取文件路径

该文章转载自:

拖曳文件获取文件路径

 

 

//拖拽获取文件路径
package test;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
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.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * This is a test class to test drag and drop behavior. Drop items into the text
 * area to see the MIME types of the drop target.
 */
public class Demo2 {
    public static void main(String[] args) {
        JFrame frame = new DropTargetFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

/**
 * This frame contains a text area that is a simple drop target.
 */
class DropTargetFrame extends JFrame {
    public DropTargetFrame() {
        setTitle("DropTarget");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        JTextArea textArea = new JTextArea("Drop items into this text area.\n");

        new DropTarget(textArea, new TextDropTargetListener(textArea));
        add(new JScrollPane(textArea), "Center");
    }

    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 300;
}

/**
 * This listener displays the properties of a dropped object.
 */
class TextDropTargetListener implements DropTargetListener {
    /**
     * Constructs a listener.
     * 
     * @param aTextArea
     *            the text area in which to display the properties of the
     *            dropped object.
     */
    public TextDropTargetListener(JTextArea aTextArea) {
        textArea = aTextArea;
    }

    public void dragEnter(DropTargetDragEvent event) {
        int a = event.getDropAction();
        if ((a & DnDConstants.ACTION_COPY) != 0)
            textArea.append("ACTION_COPY\n");
        if ((a & DnDConstants.ACTION_MOVE) != 0)
            textArea.append("ACTION_MOVE\n");
        if ((a & DnDConstants.ACTION_LINK) != 0)
            textArea.append("ACTION_LINK\n");

        if (!isDragAcceptable(event)) {
            event.rejectDrag();
            return;
        }
    }

    public void dragExit(DropTargetEvent event) {
    }

    public void dragOver(DropTargetDragEvent event) {
        // you can provide visual feedback here
    }

    public void dropActionChanged(DropTargetDragEvent event) {
        if (!isDragAcceptable(event)) {
            event.rejectDrag();
            return;
        }
    }

    public void drop(DropTargetDropEvent event) {
        if (!isDropAcceptable(event)) {
            //拒绝 Drop。
            event.rejectDrop();
            return;
        }

        event.acceptDrop(DnDConstants.ACTION_COPY);

        Transferable transferable = event.getTransferable();

        DataFlavor[] flavors = transferable.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor d = flavors[i];
            textArea.append("MIME type=" + d.getMimeType() + "\n");

            try {
                if (d.equals(DataFlavor.javaFileListFlavor)) {
                    List<File> fileList = (List<File>) transferable.getTransferData(d);
                    
                    for (File f : fileList) {
                        textArea.append(f + "\n");
//                        System.out.println("是个文件夹");
                    }
                } else if (d.equals(DataFlavor.stringFlavor)) {
                    String s = (String) transferable.getTransferData(d);
                    textArea.append(s + "\n");
                }
            } catch (Exception e) {
                textArea.append(e + "\n");
            }
        }
        textArea.append("\n");
        event.dropComplete(true);
    }

    public boolean isDragAcceptable(DropTargetDragEvent event) {
        // usually, you check the available data flavors here
        // in this program, we accept all flavors
        return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
    }

    public boolean isDropAcceptable(DropTargetDropEvent event) {
        // usually, you check the available data flavors here
        // in this program, we accept all flavors
        return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
    }

    private JTextArea textArea;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值