使用以下属性配置您的表:
tblDocuments.setDragEnabled(true);
tblDocuments.setTransferHandler(new FileTransferHandler());
这是 TransferHandler 的实现:
public class FileTransferHandler extends TransferHandler {
@Override
protected Transferable createTransferable(JComponent c) {
List<File> files = new ArrayList<File>();
// copy your files from the component to a concrete List<File> files ...
// the following code would be a sample for a JList filled with java.io.File(s) ...
/*JList list = (JList) c;
for (Object obj: list.getSelectedValues()) {
files.add((File)obj);
}*/
return new FileTransferable(files);
}
@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
}
这是 FileTransferable 的实现:
public class FileTransferable implements Transferable {
private List<File> files;
public FileTransferable(List<File> files) {
this.files = files;
}
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.javaFileListFlavor};
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(DataFlavor.javaFileListFlavor);
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return files;
}
}