I didn't change my code, but drag and drop recently stopped working to external applications (Firefox/Chrome browsers, dolphin, etc.) from a java Swing application running on Ubuntu. We think this may have changed after an Ubuntu upgrade.
The problem happens dropping onto Firefox/Google Chrome or onto file explorers on Ubuntu. The problem doesn't affect drag and drop within our Swing application.
When a drag is initiated, you can see the icon for dragging correctly shows, but when you drag over any of the drop locations, they show a red cancel type icon showing you can't drop there. Letting go of the mouse at that point does nothing in the java application code.
What needs to be changed in our java app to support what is expected? Drag and drop still works fine within the java app.
Here is the java code used to support a Transfer:
public static DataFlavor uriListFlavor; // initialized elsewhere as new DataFlavor("text/uri-list;class=java.lang.String");
JTable docsTable; // initialized elsewhere
docsTable.setTransferHandler(new DocTransferHandler());
//etc.
public static class DocTransferHandler extends TransferHandler {
DocTransferHandler() {
}
@Override
protected Transferable createTransferable(JComponent component) {
ArrayList<File> files = new ArrayList<>();
if (component instanceof JTable) {
JTable table = (JTable)component;
int[] rows = table.getSelectedRows();
for (int row : rows) {
String filePath = String.valueOf(table.getTModel().getValueAt(row, FILE_PATH_COLUMN));
String fileName = String.valueOf(table.getTModel().getValueAt(row, FILE_NAME_COLUMN));
files.add(new File(filePath+"/"+fileName));
}
}
return new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] flavors = new DataFlavor[2];
flavors[0] = DataFlavor.javaFileListFlavor;
flavors[1] = uriListFlavor;
return flavors;
}
@Override
public boolean canImport(TransferSupport support) {
return (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || support.isDataFlavorSupported(uriListFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (flavor.equals(CaseManagedDocs.uriListFlavor)) {
String uriList = "";
for (File file : files) {
uriList += file.toURI() + System.lineSeparator();
}
return uriList;
} else if (flavor.equals(DataFlavor.javaFileListFlavor)) {
return files;
}
throw new UnsupportedFlavorException(flavor);
}
};
}