示例程序-
在一个示例程序中,我有一个JTable
显示文件列表的程序。当我将该文件拖放到本机文件系统中的任何位置时,我的程序将该文件移动到放置的位置。
在示例程序的幕后,我在系统位置创建了一个同名的临时文件,temp
并将内容写入原始拖动文件的临时文件中。完成后,临时文件会自动移动到放置的位置。
要运行示例程序,请创建一个txt
文件,abc
插入一些文本并将该文件复制到当前工作目录中。
这是代码-
package com.main;
import java.awt.Dimension;
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author admin
*/
public class DragAndDropJavaApplicationMainClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JavaApplicationPanel applicationPanel = new JavaApplicationPanel();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(applicationPanel);
Dimension dim = new Dimension(450, 225);
frame.setPreferredSize(dim);
frame.setMinimumSize(dim);
frame.pack();
frame.setVisible(true);
}
}
FileDragGestureListener.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.main;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceAdapter;
import java.awt.dnd.DragSourceContext;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;
/**
*
* @author admin
*/
public class FileDragGestureListener extends DragSourceAdapter implements DragGestureListener {
Cursor cursor;
String txt = "temp";
@Override
public void dragGestureRecognized(DragGestureEvent dge) {
try {
File proxy_temp = File.createTempFile("tempdir", ".dir", null);
File temp = new File(proxy_temp.getParent(), "abc.txt");
File file = new File("abc.txt");
Scanner sc = new Scanner(file);
PrintWriter printer = new PrintWriter(temp);
while (sc.hasNextLine()) {
String s = sc.nextLine();
printer.write(s);
}
printer.flush();
printer.close();
FileSystemView fsv = FileSystemView.getFileSystemView();
Icon icn = fsv.getSystemIcon(temp);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getBestCursorSize(icn.getIconWidth(), icn.getIconHeight());
BufferedImage buff = new BufferedImage(dim.width, dim.height,
BufferedImage.TYPE_INT_ARGB);
if (DragSource.isDragImageSupported()) {
dge.startDrag(DragSource.DefaultCopyDrop, buff, new Point(0, 0),
new TextFileTransferable(temp),
this);
} else {
cursor = tk.createCustomCursor(buff, new Point(0, 0), "sString");
dge.startDrag(cursor, null, new Point(0, 0),
new TextFileTransferable(temp),
this);
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
@Override
public void dragEnter(DragSourceDragEvent dsde) {
DragSourceContext dragSourceContext = dsde.getDragSourceContext();
dragSourceContext.setCursor(cursor);
}
@Override
public void dragExit(DragSourceEvent dse) {
DragSourceContext dragSourceContext = dse.getDragSourceContext();
dragSourceContext.setCursor(DragSource.DefaultCopyNoDrop);
}
}
JavaApplicationPanel.java
package com.main;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author admin
*/
public class JavaApplicationPanel extends javax.swing.JPanel {
/**
* Creates new form JavaApplicationPanel
*/
public JavaApplicationPanel() {
initComponents();
DragSource ds = DragSource.getDefaultDragSource();
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(jTable,
DnDConstants.ACTION_MOVE, new FileDragGestureListener());
}
/**
* 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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable = new javax.swing.JTable();
jTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"abc.txt", "1 KB", "Tuesday, January 29, 2013, 12:09:24 PM", ""}
},
new String [] {
"File Name", "Size", "Last Modified", "Download Location"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jTable);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 632, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTable jTable;
// End of variables declaration
}
TextFileTransferable.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.main;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author admin
*/
public class TextFileTransferable implements Transferable {
File temp;
public TextFileTransferable(File temp) throws IOException {
this.temp = temp;
}
@Override
public Object getTransferData(DataFlavor flavor) {
List list = new ArrayList();
list.add(temp);
return list;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] df = new DataFlavor[1];
df[0] = DataFlavor.javaFileListFlavor;
return df;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (flavor == DataFlavor.javaFileListFlavor) {
return true;
}
return false;
}
}
我的示例程序中唯一的问题是我需要知道该临时文件最终从系统temp
位置移动到实际放置位置的位置。即用户在本机文件系统中实际删除了该文件的位置。