拖拽文件获取文件路径

来源:互联网 发布:江西百川网络托管 编辑:程序博客网 时间:2024/05/22 23:20

该文章转载自:

拖曳文件获取文件路径

 

 

//拖拽获取文件路径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;}


 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 羊肉煮熟了太硬怎么办 切菜不小心切到手指怎么办 打荷盘子端错了怎么办 学厨师不会翻锅怎么办? 在饭店上班不给工资怎么办 炒菜的左手拿不起锅怎么办?? 厨师想尽快上手炒菜怎么办? 空调吹的肩膀痛怎么办 学厨师翻锅不会怎么办 肋软骨隆鼻太硬怎么办 百度云下载速度慢怎么办 买的种子没出怎么办 铃木小r烧整流器怎么办 厨师在厨房太热怎么办 被家长投诉体罚孩子怎么办 空腹彩超喝水了怎么办 鱼把厕.所堵了怎么办 螃蟹爬进厕所里怎么办 抄作业被老师发现怎么办 鱼把厕所堵了怎么办 刚憋尿上完厕所之后腹部很疼怎么办 小孩被老师投诉了家长怎么办 家长投诉被老师知道了怎么办 孩子上课不敢回答问题怎么办 孩子犯了错家长怎么办 错了不该错的题怎么办 不该错的题错了怎么办 小学生不爱写课堂作业怎么办 手被老师打肿了怎么办 老师反应孩子学习退步了怎么办 学护理的打屁股针怎么办 教师被投诉打学生怎么办 遇到内向的学生教师应该怎么办 教师遇到顽劣的学生怎么办 家长质疑老师的能力怎么办 和领导有冲突该怎么办 孩子叫也不听特别叛逆怎么办 孩子叛逆期不听妈妈的话怎么办 学生和老师反嘴怎么办? 两个月的宝宝不拉屎怎么办 老师受家长的气怎么办