java 鼠标拖放文件的实现

 在最近作的聊天工具中,
希望追加文件传输的功能。

基本思想是:
将文件或文件夹拖拽到聊天对话框后,
释放鼠标激活drop事件后,
可以得到被选择的文件或文件夹的信息,
以便进行数据的传输。

在网络中搜索了一番后,
发现有一篇文章非常适合我的需求。

下文转自《java 鼠标拖放文件的实现》
原文:http://hi.baidu.com/netbusy/blog/item/11fd2c4cec09b3fdd72afc75.html



/*
* NewJFrame.java
*
* Created on 2008年2月2日, 上午11:06
*/

package yyplayer;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
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.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.swing.JTextArea;

/**
*
* @author Administrator
*/
public class NewJFrame. extends javax.swing.JFrame. {
   
    /** Creates new form. NewJFrame. */
    public NewJFrame() {
        initComponents();
    }
   
    /** 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.
     */
    //                          
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new MyTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }//
                       
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
   
    class MyTextArea extends JTextArea implements DropTargetListener{
        public MyTextArea(){
            new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
        }

        public void dragEnter(DropTargetDragEvent dtde) {
        }

        public void dragOver(DropTargetDragEvent dtde) {
        }

        public void dropActionChanged(DropTargetDragEvent dtde) {
        }

        public void dragExit(DropTargetEvent dte) {
        }

        public void drop(DropTargetDropEvent dtde) {
            try {
                Transferable tr = dtde.getTransferable();

                if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                    System.out.println("file cp");
                    List list = (List) (dtde.getTransferable()
                            .getTransferData(DataFlavor.javaFileListFlavor));
                    Iterator iterator = list.iterator();
                    while (iterator.hasNext()) {
                        File f = (File) iterator.next();
                        this.setText(f.getAbsolutePath());
                    }
                    dtde.dropComplete(true);
                    this.updateUI();
                }else {
                    dtde.rejectDrop();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (UnsupportedFlavorException ufe) {
                ufe.printStackTrace();
            }

        }
       
    }
    // 变量声明 - 不进行修改                    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // 变量声明结束                  
   
}

你可能感兴趣的:(JAVA)