I am using the following code that I found (How can I use Drag-and-Drop in Swing to get file path? from ABika) to do drag and drop:
final class FileDropHandler extends TransferHandler {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
for (DataFlavor flavor : support.getDataFlavors()) {
if (flavor.isFlavorJavaFileListType()) {
return true;
}
}
return false;
}
@Override
@SuppressWarnings("unchecked")
public boolean importData(TransferHandler.TransferSupport support) {
if (!this.canImport(support))
return false;
List<File> files;
try {
files = (List<File>) support.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor);
} catch (UnsupportedFlavorException | IOException ex) {
// should never happen (or JDK is buggy)
return false;
}
for (File file: files) {
// do something...
}
return true;
}
}
The handler is then added to a component.
But the problem is that despite the "@SuppressWarnings("unchecked")", I am getting an error:
The type List is not generic; it cannot be parameterized with arguments < File>
Can anyone tell me what is wrong here? It seems so straight-forward. Thanks
The method getTransferData
is supposed to return a java.util.List
in this case but you are importing java.awt.List
, either as a single-type import statement i.e. import java.awt.List;
or as an import-on-demand statement i.e. import java.awt.*;
. If you have the former, you need to change it to java.util.List
and if you have the latter, you need to import every class from java.awt
as a single-type import instead, or use a qualified List
type name in the importData
method, like java.util.List<File>
.