Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dotob
Created August 15, 2012 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotob/3363215 to your computer and use it in GitHub Desktop.
Save dotob/3363215 to your computer and use it in GitHub Desktop.
filedragdrop handler
// imports omitted, dont know why...
public class Foo extends TransferHandler {
//.../*DRAG & DROP*/
@Override
public boolean importData(JComponent comp, Transferable t) {
// Make sure we have the right starting points
if (!(comp instanceof JTextField)) {
return false;
}
if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
return false;
}
// Grab the tree, its model and the root node
JTextField textField = (JTextField) comp;
try {
List data = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
for (Object object : data) {
File f = (File) object;
textField.setText(f.getAbsolutePath());
}
return true;
} catch (UnsupportedFlavorException ufe) {
System.err.println("Ack! we should not be here.\nBad Flavor.");
} catch (IOException ioe) {
System.out.println("Something failed during import:\n" + ioe);
}
return false;
}
@Override
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
if (comp instanceof JTextField) {
for (DataFlavor transferFlavor : transferFlavors) {
if (transferFlavor.equals(DataFlavor.javaFileListFlavor)) {
return true;
}
}
return false;
}return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment