Package sc.fiji.snt.gui
Class FileDrop
java.lang.Object
sc.fiji.snt.gui.FileDrop
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, rob@iharder.net
Additional support:
- September 2007, Nathan Blomquist -- Linux (KDE/Gnome) support added.
- December 2010, Joshua Gerth
- June 2019, TF, Adjust defaultBorderColor. Code cleanup. Added the ability to abort drop operation using Esc
- Version:
- 1.1.1
- Author:
- Robert Harder, rharder@users.sf.net
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceImplement this inner interface to listen for when files are dropped. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFileDrop(Component c, boolean recursive, FileDrop.Listener listener) Constructor with a default border and the option to recursively set drop targets.protectedFileDrop(Component c, Border dragBorder, boolean recursive, FileDrop.Listener listener) Constructor with a specified border and the option to recursively set drop targets.protectedFileDrop(Component c, Border dragBorder, FileDrop.Listener listener) Constructor with a specified borderFileDrop(Component c, FileDrop.Listener listener) protectedFileDrop(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.protectedFileDrop(PrintStream out, Component c, Border dragBorder, boolean recursive, FileDrop.Listener listener) Full constructor with a specified border and debugging optionally turned on.protectedFileDrop(PrintStream out, Component c, Border dragBorder, FileDrop.Listener listener) Constructor with a specified border and debugging optionally turned on.protectedFileDrop(PrintStream out, Component c, FileDrop.Listener listener) Constructor with a default border and debugging optionally turned on. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanRemoves the drag-and-drop hooks from the component and optionally from the all children.protected static booleanremove(PrintStream out, Component c, boolean recursive) Removes the drag-and-drop hooks from the component and optionally from the all children.
-
Constructor Details
-
FileDrop
Constructs aFileDropwith a default light-blue border and, if c is aContainer, recursively sets all elements contained within as drop targets, though only the top level container will change borders.- Parameters:
c- Component on which files will be dropped.listener- Listens forfilesDropped.- Since:
- 1.0
-
FileDrop
Constructor with a default border and the option to recursively set drop targets. If your component is ajava.awt.Container, then each of its children components will also listen for drops, though only the parent will change borders.- Parameters:
c- Component on which files will be dropped.recursive- Recursively set children as drop targets.listener- Listens forfilesDropped.- Since:
- 1.0
-
FileDrop
Constructor with a default border and debugging optionally turned on. With Debugging turned on, more status messages will be displayed toout. A common way to use this constructor is withSystem.outorSystem.err. Anullvalue for the parameteroutwill result in no debugging output.- Parameters:
out- PrintStream to record debugging info or null for no debugging.c- Component on which files will be dropped.listener- Listens forfilesDropped.- Since:
- 1.0
-
FileDrop
Constructor with a default border, debugging optionally turned on and the option to recursively set drop targets. If your component is ajava.awt.Container, then each of its children components will also listen for drops, though only the parent will change borders. With Debugging turned on, more status messages will be displayed toout. A common way to use this constructor is withSystem.outorSystem.err. Anullvalue for the parameteroutwill result in no debugging output.- Parameters:
out- PrintStream to record debugging info or null for no debugging.c- Component on which files will be dropped.recursive- Recursively set children as drop targets.listener- Listens forfilesDropped.- Since:
- 1.0
-
FileDrop
Constructor with a specified border- Parameters:
c- Component on which files will be dropped.dragBorder- Border to use onJComponentwhen dragging occurs.listener- Listens forfilesDropped.- Since:
- 1.0
-
FileDrop
Constructor with a specified border and the option to recursively set drop targets. If your component is ajava.awt.Container, then each of its children components will also listen for drops, though only the parent will change borders.- Parameters:
c- Component on which files will be dropped.dragBorder- Border to use onJComponentwhen dragging occurs.recursive- Recursively set children as drop targets.listener- Listens forfilesDropped.- Since:
- 1.0
-
FileDrop
Constructor with a specified border and debugging optionally turned on. With Debugging turned on, more status messages will be displayed toout. A common way to use this constructor is withSystem.outorSystem.err. Anullvalue for the parameteroutwill result in no debugging output.- Parameters:
out- PrintStream to record debugging info or null for no debugging.c- Component on which files will be dropped.dragBorder- Border to use onJComponentwhen dragging occurs.listener- Listens forfilesDropped.- Since:
- 1.0
-
FileDrop
protected FileDrop(PrintStream out, Component c, Border dragBorder, boolean recursive, FileDrop.Listener listener) Full constructor with a specified border and debugging optionally turned on. With Debugging turned on, more status messages will be displayed toout. A common way to use this constructor is withSystem.outorSystem.err. Anullvalue for the parameteroutwill result in no debugging output.- Parameters:
out- PrintStream to record debugging info or null for no debugging.c- Component on which files will be dropped.dragBorder- Border to use onJComponentwhen dragging occurs.recursive- Recursively set children as drop targets.listener- Listens forfilesDropped.- Since:
- 1.0
-
-
Method Details
-
remove
Removes the drag-and-drop hooks from the component and optionally from the all children. You should call this if you add and remove components after you've set up the drag-and-drop. This will recursively unregister all components contained within c if c is aContainer.- Parameters:
c- The component to unregister as a drop target- Since:
- 1.0
-
remove
Removes the drag-and-drop hooks from the component and optionally from the all children. You should call this if you add and remove components after you've set up the drag-and-drop.- Parameters:
out- OptionalPrintStreamfor logging drag and drop messagesc- The component to unregisterrecursive- Recursively unregister components within a container- Since:
- 1.0
-