我有一个JList
启用拖放功能。现在,我将 Java 项目中一个包中的文件添加到JList
a 中,让它们在前面显示它的图标。现在,如果我从桌面 D&DJList
到桌面,它工作得很好,但如果我创建一个JAR
文件(或使用 EXE 文件launch4j
)拖放 JAR 中的文件不再起作用。有什么解决办法吗?这是我Transferable
和TransferHandler
设置为 JList 的类:
public class TransferableFile implements Transferable
{
private List<File> fileList;
public TransferableFile(List<File> files)
{
fileList = files;
}
@Override
public DataFlavor[] getTransferDataFlavors()
{
return new DataFlavor[] { DataFlavor.javaFileListFlavor };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return flavor.equals(DataFlavor.javaFileListFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
if (flavor.equals(DataFlavor.javaFileListFlavor))
return fileList;
throw new UnsupportedFlavorException(flavor);
}
}
public class FileListTransferHandler extends TransferHandler
{
@Override
public int getSourceActions(JComponent c)
{
return COPY;
}
@Override
protected Transferable createTransferable(JComponent c)
{
@SuppressWarnings("unchecked")
JList<File> list = (JList<File>) c;
File value = (File) list.getSelectedValue();
Vector<File> files = new Vector<>();
String listEntry;
listEntry = value.getAbsolutePath();
JOptionPane.showMessageDialog(null, listEntry);
files.add(new File(listEntry));
TransferableFile tf = new TransferableFile(files);
return tf;
}
}