Making new Flavors : Making a custom transferhandler for and drop applications

Discussion in 'Java' started by ebby83@gmail.com, Jan 12, 2005.

  1. Guest

    Hi all .. im trying to make a simple application thru which i can
    accept files of any kind via a drag and drop interface .

    I tried making my own transferhandler , but it gives a compilation
    problem saying
    cannot parse java.io.File

    here is the code

    --------------------------
    //FileTransferHandler.java

    package com.tcs.search.classes;

    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.datatransfer.*;

    public class FileTransferHandler extends javax.swing.TransferHandler {
    DataFlavor fileFlavor ;

    public FileTransferHandler() {
    try {
    fileFlavor = new DataFlavor("java.io.File");
    }
    catch(ClassNotFoundException c)
    { System.out.println(c.getMessage()); }
    }

    public boolean importData(JComponent c, Transferable t)
    {
    if (hasFileFlavor(t.getTransferDataFlavors())) {
    try {
    File f = (File)t.getTransferData(fileFlavor);
    return true;
    } catch (UnsupportedFlavorException ufe) {
    } catch (IOException ioe) { }
    }
    return false;
    }


    protected boolean hasFileFlavor(DataFlavor[] flavors)
    {
    if (fileFlavor == null) {
    return false;
    }

    for (int i = 0; i < flavors.length; i++) {
    if (fileFlavor.equals(flavors)) {
    return true;
    }
    }
    return false;
    }
    }

    --------------------------
    //FrontLauncher.java

    package com.tcs.search;

    import javax.swing.*;
    import java.awt.*;
    import com.tcs.search.classes.*;

    public class FrontLauncher extends JFrame {

    public FrontLauncher() {


    Container c = this.getContentPane();
    JEditorPane ep = new JEditorPane();
    c.add(ep);
    ep.setDragEnabled(true);
    //ep.setDropTarget(this);

    ep.setTransferHandler(new FileTransferHandler());
    }

    public static void main(String[] args) {
    FrontLauncher frame = new FrontLauncher();
    frame.setSize(500, 400);
    frame.setTitle("FrontLauncher");
    frame.setVisible(true);
    }


    }

    --------------------------
    , Jan 12, 2005
    #1
    1. Advertising

  2. Andrew Thompson, Jan 12, 2005
    #2
    1. Advertising

  3. Guest

    Well I tried some changes .. and this version works ( well it compiled
    ) but I still am unable to drag and drop files from the system to this
    application

    FrontLauncher.java ::
    -----------------------------
    package com.tcs.search;

    import javax.swing.*;
    import java.awt.*;
    import com.tcs.search.classes.*;

    public class FrontLauncher extends JFrame {

    public FrontLauncher()
    {
    Container c = this.getContentPane();
    JEditorPane ep = new JEditorPane();
    c.add(ep);
    ep.setDragEnabled(true);


    ep.setTransferHandler(new FileTransferHandler());
    }

    public static void main(String[] args) {
    FrontLauncher frame = new FrontLauncher();
    frame.setSize(500, 400);
    frame.setTitle("FrontLauncher");
    frame.setVisible(true);
    }
    }
    -----------------------------



    FileTransferHandler.java::
    -----------------------------
    package com.tcs.search.classes;

    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.datatransfer.*;
    import java.util.*;

    public class FileTransferHandler extends javax.swing.TransferHandler {
    DataFlavor fileFlavor ;

    public FileTransferHandler()
    {
    fileFlavor = DataFlavor.javaFileListFlavor;
    }

    public boolean importData(JComponent c, Transferable t)
    {
    if (hasFileFlavor(t.getTransferDataFlavors())) {
    try {
    java.util.List fileList =
    (java.util.List)t.getTransferData(fileFlavor);
    return true;
    } catch (UnsupportedFlavorException ufe) {
    } catch (IOException ioe) { }
    }
    return false;
    }


    protected boolean hasFileFlavor(DataFlavor[] flavors)
    {
    if (fileFlavor == null) {
    return false;
    }

    for (int i = 0; i < flavors.length; i++) {
    if (fileFlavor.equals(flavors)) {
    return true;
    }
    }
    return false;
    }
    }
    -----------------------------

    PS -- Sorry about the format of the CODE , but when i post the message
    part of the indentation mysteriously dissapears !!!
    , Jan 12, 2005
    #3
  4. On 12 Jan 2005 01:15:55 -0800, wrote:

    (snip horridly unindented code)

    > } catch (UnsupportedFlavorException ufe) {
    > } catch (IOException ioe) { }
    > }


    (Begin to) find out what is happening..
    <http://www.physci.org/codes/javafaq.jsp#stacktrace>

    > PS -- Sorry about the format of the CODE , but when i post the message
    > part of the indentation mysteriously dissapears !!!


    Please change your software or get it fixed* by the manufacturer.

    [ * Please lodge a bug report. ]

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jan 12, 2005
    #4
  5. Guest

    its googles fault ... not my editor or any other software ...
    , Jan 12, 2005
    #5
  6. On 12 Jan 2005 03:00:55 -0800, wrote:

    > its googles fault ...


    I realise that Google is the source of the problem.

    [ But as an aside, you can also change usenet internet-portal
    and stop the problem from happening. It is ultimately *your*
    decision as to whether to continue using such a broken
    interface, (and potentially get less answers,) or change
    to another provider that does not cause the problem. ]

    >...not my editor or any other software ...


    So *report* it to *Google*. (Please)

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Jan 12, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Thompson

    Detecting Unix flavors

    Andrew Thompson, Sep 5, 2004, in forum: Java
    Replies:
    21
    Views:
    1,706
  2. Jeff Thies

    OT: CSV flavors

    Jeff Thies, Jul 21, 2004, in forum: HTML
    Replies:
    2
    Views:
    420
    Jeff Thies
    Jul 22, 2004
  3. kbutterly
    Replies:
    3
    Views:
    384
    kbutterly
    Jan 30, 2007
  4. Michael Olea

    What to name container "flavors"?

    Michael Olea, Jun 30, 2005, in forum: C++
    Replies:
    2
    Views:
    312
    Michael Olea
    Jul 1, 2005
  5. Christopher
    Replies:
    2
    Views:
    265
    James Kanze
    Feb 1, 2008
Loading...

Share This Page