拖入文件的窗口

package com.tntxia.swingcommon;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
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.Iterator;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.tntxia.commonswing.MainFrame;

public class DragInFileFrame extends MainFrame implements DropTargetListener{
    
    private JTextField source = new JTextField(50);
    
    private JButton buttonConfirm = new JButton();
    
    private JLabel label = new JLabel();
    
    private Box box = Box.createVerticalBox();
    
    public JTextField getSource()
    {
        return source;
    }

    public void setSource(JTextField source)
    {
        this.source = source;
    }

    public JButton getButtonConfirm()
    {
        return buttonConfirm;
    }

    public void setButtonConfirm(JButton buttonConfirm)
    {
        this.buttonConfirm = buttonConfirm;
    }
    
    public JLabel getLabel()
    {
        return label;
    }

    public void setLabel(JLabel label)
    {
        this.label = label;
    }
    
    public Box getBox()
    {
        return box;
    }

    public void setBox(Box box)
    {
        this.box = box;
    }
    
    public DragInFileFrame(){
        
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new FlowLayout());
        
        JPanel panel = new JPanel();
        
        panel.add(label);
        panel.add(source);
        box.add(panel);
        
        JLabel labelTip = new JLabel("可以手动输入路径 ,也可以把文件拖放到这个窗口");
        box.add(labelTip);
        
        // 设置拖入文件
        new DropTarget(contentPane, 3, this);
        
        // 设置拖入文件
        new DropTarget(source, 3, this);
        
        panel.add(buttonConfirm);
        contentPane.add(box);
    }

    /**
     * 
     */
    private static final long serialVersionUID = 3557138117709489934L;

    @Override
    public void dragEnter(DropTargetDragEvent dtde) {
        
    }

    @Override
    public void dragOver(DropTargetDragEvent dtde) {
        
    }

    @Override
    public void dropActionChanged(DropTargetDragEvent dtde) {
        
    }

    @Override
    public void dragExit(DropTargetEvent dte) {
        
    }

    @Override
    public void drop(DropTargetDropEvent dtde) {
        Iterator iterator;
        dtde.acceptDrop(dtde.getDropAction());  
        try {
            iterator = ((List)dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor)).iterator();
            int numFiles = 0;
            File inFile = null;
            while(iterator.hasNext())
            {
                if(numFiles++ == 0)
                {
                    inFile = (File)iterator.next();
                    continue;
                }
                break;
            }
            
            String filePath = inFile.getAbsolutePath();
            source.setText(filePath);
            
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 入口函数
     * @param args
     */
    public static void main(String args[]){
        
    }

}

 

你可能感兴趣的:(文件)