public class FileDrop extends Object
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 javax.swing.border.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
Modifier and Type | Class and Description |
---|---|
static interface |
FileDrop.Listener
Implement this inner interface to listen for when files are dropped.
|
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.
|
Constructor and Description |
---|
FileDrop(Component c,
boolean recursive,
FileDrop.Listener listener)
Constructor with a default border and the option to recursively set drop targets.
|
FileDrop(Component c,
Border dragBorder,
boolean recursive,
FileDrop.Listener listener)
Constructor with a specified border and the option to recursively set drop targets.
|
FileDrop(Component c,
Border dragBorder,
FileDrop.Listener listener)
Constructor with a specified border
|
FileDrop(Component c,
FileDrop.Listener listener)
|
FileDrop(PrintStream out,
Component c,
boolean recursive,
FileDrop.Listener listener)
Constructor with a default border, debugging optionally turned on
and the option to recursively set drop targets.
|
FileDrop(PrintStream out,
Component c,
Border dragBorder,
boolean recursive,
FileDrop.Listener listener)
Full constructor with a specified border and debugging optionally turned on.
|
FileDrop(PrintStream out,
Component c,
Border dragBorder,
FileDrop.Listener listener)
Constructor with a specified border and debugging optionally turned on.
|
FileDrop(PrintStream out,
Component c,
FileDrop.Listener listener)
Constructor with a default border and debugging optionally turned on.
|
Modifier and Type | Method and Description |
---|---|
private boolean |
isDraggedFileList(PrintStream out,
DropTargetDragEvent evt)
Determine if the dragged data is a file list.
|
private boolean |
isDroppedFileList(PrintStream out,
DropTargetDropEvent evt)
Determine if the dropped data is a file list.
|
private static void |
log(PrintStream out,
String message)
Outputs message to out if it's not null.
|
static void |
main(String[] args)
Runs a sample program that shows dropped files
|
private void |
makeDropTarget(PrintStream out,
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(PrintStream out,
Component c,
boolean recursive)
Removes the drag-and-drop hooks from the component and optionally
from the all children.
|
private static boolean |
supportsDnD() |
private transient Border normalBorder
private transient DropTargetListener dropListener
private static Boolean supportsDnD
private static Color defaultBorderColor
public FileDrop(Component c, FileDrop.Listener listener)
FileDrop
with a default light-blue border
and, if c is a Container
, recursively
sets all elements contained within as drop targets, though only
the top level container will change borders.c
- Component on which files will be dropped.listener
- Listens for filesDropped.public FileDrop(Component c, boolean recursive, FileDrop.Listener listener)
c
- Component on which files will be dropped.recursive
- Recursively set children as drop targets.listener
- Listens for filesDropped.public FileDrop(PrintStream out, Component c, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.out
- c
- Component on which files will be dropped.listener
- Listens for filesDropped.public FileDrop(PrintStream out, Component c, boolean recursive, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.out
- c
- Component on which files will be dropped.recursive
- Recursively set children as drop targets.listener
- Listens for filesDropped.public FileDrop(Component c, Border dragBorder, FileDrop.Listener listener)
c
- Component on which files will be dropped.dragBorder
- Border to use on JComponent when dragging occurs.listener
- Listens for filesDropped.public 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.public FileDrop(PrintStream out, Component c, Border dragBorder, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.c
- Component on which files will be dropped.dragBorder
- Border to use on JComponent when dragging occurs.listener
- Listens for filesDropped.public FileDrop(PrintStream out, Component c, Border dragBorder, boolean recursive, FileDrop.Listener listener)
out
- PrintStream to record debugging info or null for no debugging.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 void makeDropTarget(PrintStream out, Component c, boolean recursive)
private boolean isDroppedFileList(PrintStream out, DropTargetDropEvent evt)
private boolean isDraggedFileList(PrintStream out, DropTargetDragEvent evt)
private static void log(PrintStream out, String message)
public static boolean remove(Component c)
Container
.c
- The component to unregister as a drop targetpublic static boolean remove(PrintStream out, Component c, boolean recursive)
out
- Optional PrintStream
for logging drag and drop messagesc
- The component to unregisterrecursive
- Recursively unregister components within a containerWebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses