Class FileDrop


  • public class FileDrop
    extends java.lang.Object
    A support class that allows interface components to easily accept files being dropped on them from other components or from the platform.
    Since:
    1.61
    Author:
    Adapted from public domain code by Robert Harder and Nathan Blomquist
    • Constructor Summary

      Constructors 
      Constructor Description
      FileDrop​(java.awt.Component component, FileDrop.Listener listener)
      Creates a new FileDrop on the component.
      FileDrop​(java.awt.Component component, javax.swing.JComponent borderOwner, boolean attachToDescendants)
      Creates a new FileDrop on the component.
      FileDrop​(java.awt.Component component, javax.swing.JComponent borderOwner, boolean attachToDescendants, FileDrop.Listener listener)
      Creates a new FileDrop on the component.
      FileDrop​(java.awt.Component component, javax.swing.JComponent borderOwner, FileDrop.Listener listener)
      Creates a new FileDrop on the component.
      FileDrop​(javax.swing.JComponent borderOwner, java.awt.Component[] components)
      Creates a new FileDrop on the specified components.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.io.FileFilter getFileFilter()
      Returns the file filter for this drop handler.
      static FileDrop of​(javax.swing.JComponent borderOwner, java.awt.Component... components)
      Creates and returns a new FileDrop on the specified components.
      static void remove​(java.awt.Component component, boolean recursive)
      Removes all drag-and-drop support from a component and optionally from its descendants.
      void setFileFilter​(java.io.FileFilter fileFilter)
      Sets the file filter for this drop handler.
      void setListener​(FileDrop.DropListener complexListener)
      Sets the drop listener to receive detailed information about dropped files.
      void setListener​(FileDrop.Listener simpleListener)
      Sets the drop listener to a simple listener that receives a non-null, non-empty array of files.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileDrop

        public FileDrop​(java.awt.Component component,
                        javax.swing.JComponent borderOwner,
                        boolean attachToDescendants)
        Creates a new FileDrop on the component. If the component is a JComponent, a DropBorder will appear when dragging files over it.
        Parameters:
        component - the component that will accept files
        borderOwner - if non-null, a component whose border will change to indicate that files are being dragged over the component
        attachToDescendants - if true, then all of the descendants of the component will also listen for file drops
      • FileDrop

        public FileDrop​(java.awt.Component component,
                        FileDrop.Listener listener)
        Creates a new FileDrop on the component. If the component is a JComponent, a DropBorder will appear when dragging files over it.
        Parameters:
        component - the component that will accept files
        listener - the optional simple listener that will be notified when files are dropped
      • FileDrop

        public FileDrop​(java.awt.Component component,
                        javax.swing.JComponent borderOwner,
                        FileDrop.Listener listener)
        Creates a new FileDrop on the component. If borderOwner is not null, a DropBorder will appear when dragging files over the component.
        Parameters:
        component - the component that will accept files
        borderOwner - if non-null, a component whose border will change to indicate that files are being dragged over the component
        listener - the optional simple listener that will be notified when files are dropped
      • FileDrop

        public FileDrop​(java.awt.Component component,
                        javax.swing.JComponent borderOwner,
                        boolean attachToDescendants,
                        FileDrop.Listener listener)
        Creates a new FileDrop on the component. If the component is a JComponent, a DropBorder will appear when dragging files over it.
        Parameters:
        component - the component that will accept files
        borderOwner - if non-null, a component whose border will change to indicate that files are being dragged over the component
        attachToDescendants - if true, then all of the descendants of the component will also listen for file drops
        listener - the optional simple listener that will be notified when files are dropped
      • FileDrop

        public FileDrop​(javax.swing.JComponent borderOwner,
                        java.awt.Component[] components)
        Creates a new FileDrop on the specified components. The drop will initially have no listener attached.
        Parameters:
        borderOwner - if non-null, a component whose border will change to indicate that files are being dragged over the component
        components - the components that will accept files
        See Also:
        setListener(ca.cgjennings.ui.dnd.FileDrop.Listener)
    • Method Detail

      • of

        public static FileDrop of​(javax.swing.JComponent borderOwner,
                                  java.awt.Component... components)
        Creates and returns a new FileDrop on the specified components. The returned FileDrop will have no listener attached, but is can be set using builder-style method chaining.

        The method was added to deal with two issues: First, when this form was provided as a constrctor, it broke scripts as the script engine could not distinguish which overload to choose. Second, it avoids some compiler warnings as a common patern is to create a FileDrop in a constructor without storing a reference to it.

        Parameters:
        borderOwner - if non-null, a component whose border will change to indicate that files are being dragged over the component
        components - the components that will accept files
        See Also:
        setListener(ca.cgjennings.ui.dnd.FileDrop.Listener)
      • setListener

        public final void setListener​(FileDrop.Listener simpleListener)
        Sets the drop listener to a simple listener that receives a non-null, non-empty array of files. Only one listener may be attached to the FileDrop.
        Parameters:
        simpleListener - the listener, or null to stop receiving events
      • setListener

        public void setListener​(FileDrop.DropListener complexListener)
        Sets the drop listener to receive detailed information about dropped files. Only one listener may be attached to the FileDrop.
        Parameters:
        complexListener - the listener, or null to stop receiving events
      • setFileFilter

        public void setFileFilter​(java.io.FileFilter fileFilter)
        Sets the file filter for this drop handler. When set, dropped files will be filtered before calling the drop listener.
        Parameters:
        fileFilter - the filter to apply, or null to accept all files
      • getFileFilter

        public java.io.FileFilter getFileFilter()
        Returns the file filter for this drop handler.
        Returns:
        the file filter for this drop handler, or null
      • remove

        public static void remove​(java.awt.Component component,
                                  boolean recursive)
        Removes all drag-and-drop support from a component and optionally from its descendants.
        Parameters:
        component - the top-level component to modify
        recursive - if true, apply recursively to all descendants