Class FileDrop
- java.lang.Object
-
- org.locationtech.jtstest.testbuilder.ui.dnd.FileDrop
-
public class FileDrop extends java.lang.ObjectThis 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: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.JPanel myPanel = new JPanel(); new FileDrop( myPanel, new FileDrop.Listener() { public void filesDropped( java.io.File[] files ) { // handle file drop ... } // end filesDropped }); // end FileDrop.ListenerI'm releasing this code into the Public Domain. Enjoy.
Original author: Robert Harder, rharder@usa.net
- Version:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceFileDrop.ListenerImplement this inner interface to listen for when files are dropped.
-
Field Summary
Fields Modifier and Type Field Description private static java.awt.ColordefaultBorderColorprivate java.awt.dnd.DropTargetListenerdropListenerprivate javax.swing.border.BordernormalBorderprivate static java.lang.BooleansupportsDnDDiscover if the running JVM is modern enough to have drag and drop.
-
Constructor Summary
Constructors Constructor Description FileDrop(java.awt.Component c, boolean recursive, FileDrop.Listener listener)Constructor with a default border and the option to recursively set drop targets.FileDrop(java.awt.Component c, javax.swing.border.Border dragBorder, boolean recursive, FileDrop.Listener listener)Constructor with a specified border and the option to recursively set drop targets.FileDrop(java.awt.Component c, javax.swing.border.Border dragBorder, FileDrop.Listener listener)Constructor with a specified borderFileDrop(java.awt.Component c, FileDrop.Listener listener)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.FileDrop(java.io.PrintStream out, java.awt.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(java.io.PrintStream out, java.awt.Component c, javax.swing.border.Border dragBorder, boolean recursive, FileDrop.Listener listener)Full constructor with a specified border and debugging optionally turned on.FileDrop(java.io.PrintStream out, java.awt.Component c, javax.swing.border.Border dragBorder, FileDrop.Listener listener)Constructor with a specified border and debugging optionally turned on.FileDrop(java.io.PrintStream out, java.awt.Component c, FileDrop.Listener listener)Constructor with a default border and debugging optionally turned on.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private booleanisDragOk(java.io.PrintStream out, java.awt.dnd.DropTargetDragEvent evt)Determine if the dragged data is a file list.private static voidlog(java.io.PrintStream out, java.lang.String message)Outputs message to out if it's not null.static voidmain(java.lang.String[] args)Runs a sample program that shows dropped filesprivate voidmakeDropTarget(java.io.PrintStream out, java.awt.Component c, boolean recursive)static booleanremove(java.awt.Component c)Removes the drag-and-drop hooks from the component and optionally from the all children.static booleanremove(java.io.PrintStream out, java.awt.Component c, boolean recursive)Removes the drag-and-drop hooks from the component and optionally from the all children.private static booleansupportsDnD()
-
-
-
Field Detail
-
normalBorder
private transient javax.swing.border.Border normalBorder
-
dropListener
private transient java.awt.dnd.DropTargetListener dropListener
-
supportsDnD
private static java.lang.Boolean supportsDnD
Discover if the running JVM is modern enough to have drag and drop.
-
defaultBorderColor
private static java.awt.Color defaultBorderColor
-
-
Constructor Detail
-
FileDrop
public FileDrop(java.awt.Component c, FileDrop.Listener listener)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 for filesDropped.- Since:
- 1.0
-
FileDrop
public FileDrop(java.awt.Component c, boolean recursive, FileDrop.Listener listener)Constructor with a default border and the option to recursively set drop targets. If your component is a java.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 for filesDropped.- Since:
- 1.0
-
FileDrop
public FileDrop(java.io.PrintStream out, java.awt.Component c, FileDrop.Listener listener)Constructor with a default border and debugging optionally turned on. With Debugging turned on, more status messages will be displayed to out. A common way to use this constructor is with System.out or System.err. A null value for the parameter out will 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 for filesDropped.- Since:
- 1.0
-
FileDrop
public FileDrop(java.io.PrintStream out, java.awt.Component c, boolean recursive, FileDrop.Listener listener)Constructor with a default border, debugging optionally turned on and the option to recursively set drop targets. If your component is a java.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 to out. A common way to use this constructor is with System.out or System.err. A null value for the parameter out will 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 for filesDropped.- Since:
- 1.0
-
FileDrop
public FileDrop(java.awt.Component c, javax.swing.border.Border dragBorder, FileDrop.Listener listener)Constructor with a specified border- Parameters:
c- Component on which files will be dropped.dragBorder- Border to use on JComponent when dragging occurs.listener- Listens for filesDropped.- Since:
- 1.0
-
FileDrop
public FileDrop(java.awt.Component c, javax.swing.border.Border dragBorder, boolean recursive, FileDrop.Listener listener)Constructor with a specified border and the option to recursively set drop targets. If your component is a java.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 on JComponent when dragging occurs.recursive- Recursively set children as drop targets.listener- Listens for filesDropped.- Since:
- 1.0
-
FileDrop
public FileDrop(java.io.PrintStream out, java.awt.Component c, javax.swing.border.Border dragBorder, FileDrop.Listener listener)Constructor with a specified border and debugging optionally turned on. With Debugging turned on, more status messages will be displayed to out. A common way to use this constructor is with System.out or System.err. A null value for the parameter out will 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 on JComponent when dragging occurs.listener- Listens for filesDropped.- Since:
- 1.0
-
FileDrop
public FileDrop(java.io.PrintStream out, java.awt.Component c, javax.swing.border.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 to out. A common way to use this constructor is with System.out or System.err. A null value for the parameter out will 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 on JComponent when dragging occurs.recursive- Recursively set children as drop targets.listener- Listens for filesDropped.- Since:
- 1.0
-
-
Method Detail
-
supportsDnD
private static boolean supportsDnD()
-
makeDropTarget
private void makeDropTarget(java.io.PrintStream out, java.awt.Component c, boolean recursive)
-
isDragOk
private boolean isDragOk(java.io.PrintStream out, java.awt.dnd.DropTargetDragEvent evt)Determine if the dragged data is a file list.
-
log
private static void log(java.io.PrintStream out, java.lang.String message)Outputs message to out if it's not null.
-
remove
public static boolean remove(java.awt.Component c)
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
public static boolean remove(java.io.PrintStream out, java.awt.Component c, boolean recursive)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
-
main
public static void main(java.lang.String[] args)
Runs a sample program that shows dropped files
-
-