public class FileDrop extends Object
This class makes it easy to drag and drop files from the operating system to a Java program. Any java.awt.Component can be dropped onto, but only javax.swing.JComponents 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 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
2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
Modifier and Type | Class and Description |
---|---|
static class |
FileDrop.Event
This is the event that is passed to the
FileDropListener#filesDropped filesDropped(...) method in
your
FileDropListener when files are dropped onto
a registered drop target. |
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. |
Constructor and Description |
---|
FileDrop(Component pC,
boolean pRecursive,
FileDrop.Listener pListener)
Constructor with a default border and the option to recursively set drop targets.
|
FileDrop(Component pC,
Border pDragBorder,
boolean pRecursive,
FileDrop.Listener pListener)
Constructor with a specified border and the option to recursively set drop targets.
|
FileDrop(Component pC,
Border pDragBorder,
FileDrop.Listener pListener)
Constructor with a specified border.
|
FileDrop(Component pC,
FileDrop.Listener pListener)
|
FileDrop(PrintStream pOut,
Component pC,
boolean pRecursive,
FileDrop.Listener pListener)
Constructor with a default border, debugging optionally turned on
and the option to recursively set drop targets.
|
FileDrop(PrintStream pOut,
Component pC,
Border pDragBorder,
boolean pRecursive,
FileDrop.Listener pListener)
Full constructor with a specified border and debugging optionally turned on.
|
FileDrop(PrintStream pOut,
Component pC,
Border pDragBorder,
FileDrop.Listener pListener)
Constructor with a specified border and debugging optionally turned on.
|
FileDrop(PrintStream pOut,
Component pC,
FileDrop.Listener pListener)
Constructor with a default border and debugging optionally turned on.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
remove(Component pC)
Removes the drag-and-drop hooks from the component and optionally
from the all children.
|
static boolean |
remove(PrintStream pOut,
Component pC,
boolean pRecursive)
Removes the drag-and-drop hooks from the component and optionally
from the all children.
|
public FileDrop(Component pC, FileDrop.Listener pListener)
FileDrop
with a default light-blue border
and, if pC
is a Container
, recursively
sets all elements contained within as drop targets, though only
the top level container will change borders.pC
- Component on which files will be dropped.pListener
- Listens for filesDropped.public FileDrop(Component pC, boolean pRecursive, FileDrop.Listener pListener)
pC
- Component on which files will be dropped.pRecursive
- Recursively set children as drop targets.pListener
- Listens for filesDropped.public FileDrop(PrintStream pOut, Component pC, FileDrop.Listener pListener)
pOut
- PrintStream to record debugging info or null for no debugging.pC
- Component on which files will be dropped.pListener
- Listens for filesDropped.public FileDrop(PrintStream pOut, Component pC, boolean pRecursive, FileDrop.Listener pListener)
pOut
- PrintStream to record debugging info or null for no debugging.pC
- Component on which files will be dropped.pRecursive
- Recursively set children as drop targets.pListener
- Listens for filesDropped.public FileDrop(Component pC, Border pDragBorder, FileDrop.Listener pListener)
pC
- Component on which files will be dropped.pDragBorder
- Border to use on JComponent when dragging occurs.pListener
- Listens for filesDropped.public FileDrop(Component pC, Border pDragBorder, boolean pRecursive, FileDrop.Listener pListener)
pC
- Component on which files will be dropped.pDragBorder
- Border to use on JComponent when dragging occurs.pRecursive
- Recursively set children as drop targets.pListener
- Listens for filesDropped.public FileDrop(PrintStream pOut, Component pC, Border pDragBorder, FileDrop.Listener pListener)
pOut
- PrintStream to record debugging info or null for no debugging.pC
- Component on which files will be dropped.pDragBorder
- Border to use on JComponent when dragging occurs.pListener
- Listens for filesDropped.public FileDrop(PrintStream pOut, Component pC, Border pDragBorder, boolean pRecursive, FileDrop.Listener pListener)
pOut
- PrintStream to record debugging info or null for no debugging.pC
- Component on which files will be dropped.pDragBorder
- Border to use on JComponent when dragging occurs.pRecursive
- Recursively set children as drop targets.pListener
- Listens for filesDropped.public static boolean remove(Component pC)
pC
if pC
is a Container
.pC
- The component to unregister as a drop targetpublic static boolean remove(PrintStream pOut, Component pC, boolean pRecursive)
pOut
- Optional PrintStream
for logging drag and drop messagespC
- The component to unregisterpRecursive
- Recursively unregister components within a containerCopyright © 2023. All rights reserved.