public class FileDrop extends Object
Component
can be
dropped onto, but only JComponent
s will indicate
the drop event with a changed border.
To use this class, construct a new FileDrop by passing it the target component and a Listener to receive notification when file(s) have been dropped. Here is an example:
JPanel myPanel = new JPanel();
new FileDrop( myPanel, new FileDrop.Listener()
{ public void filesDropped( java.io.File[] files )
{
// handle file drop
...
} // end filesDropped
}); // end FileDrop.Listener
You can specify the border that will appear when files are being dragged by
calling the constructor with a Border
. Only
JComponents will show any indication with a border.
You can turn on some debugging features by passing a PrintStream object (such as System.out) into the full constructor. A null value will result in no extra debugging information being output.
I'm releasing this code into the Public Domain. Enjoy.
Original author: Robert Harder, rharder@usa.net
2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
Modifier and Type | Class and Description |
---|---|
private class |
FileDrop.DropListener |
static interface |
FileDrop.Listener
Implement this inner interface to listen for when files are dropped.
|
static class |
FileDrop.TransferableObject
At last an easy way to encapsulate your custom objects for dragging and dropping
in your Java programs!
When you need to create a
Transferable object,
use this class to wrap your object. |
Modifier and Type | Field and Description |
---|---|
private static Color |
defaultBorderColor |
private DropTargetListener |
dropListener |
private Border |
normalBorder |
private static Boolean |
supportsDnD
Discover if the running JVM is modern enough to have drag and drop.
|
private static String |
ZERO_CHAR_STRING |
Constructor and Description |
---|
FileDrop(Component c)
Constructor for JOSM file drop
|
FileDrop(Component c,
Border dragBorder,
boolean recursive,
FileDrop.Listener listener)
Full constructor with a specified border and debugging optionally turned on.
|
Modifier and Type | Method and Description |
---|---|
private static File[] |
createFileArray(BufferedReader bReader) |
private static boolean |
isDragOk(DropTargetDragEvent evt)
Determines if the dragged data is a file list.
|
private void |
makeDropTarget(Component c,
boolean recursive) |
static boolean |
remove(Component c)
Removes the drag-and-drop hooks from the component and optionally
from the all children.
|
static boolean |
remove(Component c,
boolean recursive)
Removes the drag-and-drop hooks from the component and optionally
from the all children.
|
private static boolean |
supportsDnD() |
private Border normalBorder
private DropTargetListener dropListener
private static Boolean supportsDnD
private static Color defaultBorderColor
private static final String ZERO_CHAR_STRING
public FileDrop(Component c)
c
- The drop targetpublic FileDrop(Component c, Border dragBorder, boolean recursive, FileDrop.Listener listener)
c
- Component on which files will be dropped.dragBorder
- Border to use on JComponent when dragging occurs.recursive
- Recursively set children as drop targets.listener
- Listens for filesDropped.private static boolean supportsDnD()
private static File[] createFileArray(BufferedReader bReader)
private void makeDropTarget(Component c, boolean recursive)
private static boolean isDragOk(DropTargetDragEvent evt)
evt
- the drag eventtrue
if the dragged data is a file listpublic static boolean remove(Component c)
Container
.c
- The component to unregister as a drop targettrue
if at least one item has been removed, false
otherwisepublic static boolean remove(Component c, boolean recursive)
c
- The component to unregisterrecursive
- Recursively unregister components within a containertrue
if at least one item has been removed, false
otherwise