Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cannot get TransferHandler to work in Swing

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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:


    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:


                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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Cannot get TransferHandler to work in Swing

    Seems to work for me...can you post an SSCCE that demonstrates the problem more thoroughly?

  3. The Following User Says Thank You to copeg For This Useful Post:

    pbpersson (January 31st, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot get TransferHandler to work in Swing

    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>

Similar Threads

  1. How do get and set methods work??
    By merdzins in forum Object Oriented Programming
    Replies: 2
    Last Post: December 25th, 2010, 03:17 AM
  2. home work ..
    By shane1987 in forum Collections and Generics
    Replies: 8
    Last Post: November 16th, 2010, 09:45 AM
  3. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  4. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM