Results 1 to 5 of 5
  1. #1
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default Cannot get TransferHandler to work in Swing

    I have created a Swing application and I'm trying to configure Drag and Drop support for a JTextArea. There are two capabilities I need for this object, I want it to accept text pasted into it from the clipboard and I also want to accept files dropped onto the object, grab the file name and then send that to another routine which will open the file, read the contents, and perform some work.

    However, I cannot even get past first base on this. I have created a class of transferhandler as follows:

    Java Code:
    class FileDropHandler extends TransferHandler {
    
     
        public boolean canImport(TransferSupport supp) {
            /* for the demo, we'll only support drops (not clipboard paste) */
            System.out.println("We are in FileDropHandler");
    
           //   ConsoleOutput("this is a test");
          
            if (!supp.isDrop()) {
                return false;
            }
    
            /* return true if and only if the drop contains a list of files */
            return supp.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
        }
    
    
        public boolean importData(TransferSupport supp) {
             System.out.println("We are in FileDropHandler");
            if (!canImport(supp)) {
                return false;
            }
    
            /* fetch the Transferable */
            Transferable t = supp.getTransferable();
    
            try {
                /* fetch the data from the Transferable */
                Object data = t.getTransferData(DataFlavor.javaFileListFlavor);
    
                /* data of type javaFileListFlavor is a list of files */
                java.util.List fileList = (java.util.List)data;
    
                System.out.println("Filelist " + fileList.size());
    
                /* loop through the files in the file list */
      //          for (File file : fileList) {
                    /* This is where you place your code for opening the
                     * document represented by the "file" variable.
                     * For example:
                     * - create a new internal frame with a text area to
                     *   represent the document
                     * - use a BufferedReader to read lines of the document
                     *   and append to the text area
                     * - add the internal frame to the desktop pane,
                     *   set its bounds and make it visible
                     */
    //           }
            } catch (UnsupportedFlavorException e) {
                return false;
            } catch (IOException e) {
                return false;
            }
    
            return true;
        }
     
    }
    I have created an object using that class as the type and have set the JTextArea to use that transferhandler:

    Java Code:
                txtString.setDropMode(DropMode.USE_SELECTION);
                txtString.setTransferHandler(MyTransferHandler);
    Now....if I comment out the second line above so the JTextArea uses the default transferhandler the JTextArea will accept drag and drop and the file name will appear in the JTextArea - which is not what I want.

    When I set the object to use MyTransferHandler it rejects all drag and drops.

    Please note that within the new class I have Println commands which I thought would send messages out to the console when that piece of code was executed - but nothing ever appears on the console which indicates to me the code is not being executed????

    Please help, I have spent hours on this and don't know what to try next!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

  3. #3
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    Yes, I posted the question in four different forums on the web assuming that different developers are in each forum and I would get four different answers, some of which would be easier to follow and might lead to a solution, whereas some forums might yield no answers at all.

    Are these two forums connected somehow?

  4. #4
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    This has been solved, in the code listed below I was missing the first line. It is working fine now.

    <CODE>
    MyTransferHandler = new FileDropHandler();
    txtString.setDropMode(DropMode.USE_SELECTION);
    txtString.setTransferHandler(MyTransferHandler);
    </CODE>

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pbpersson View Post
    Yes, I posted the question in four different forums on the web assuming that different developers are in each forum and I would get four different answers, some of which would be easier to follow and might lead to a solution, whereas some forums might yield no answers at all.

    Are these two forums connected somehow?
    No, but most folks mind wasting free time volunteering to help someone when the question was already answered in a cross-post. Next time if you're so inclined to cross-post, please be considerate and notify all forums involved.

Similar Threads

  1. Cant seem to get this to work
    By nivekeleets in forum Java 2D
    Replies: 4
    Last Post: 11-13-2010, 07:09 PM
  2. Replies: 0
    Last Post: 08-27-2009, 04:19 AM
  3. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  4. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •