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

E

ebby83

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);
}


}

--------------------------
 
A

Andrew Thompson

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

Please indent code (with 2 or three spaces, rather than tabs) you post.
 
E

ebby83

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;
}
}
 
A

Andrew Thompson

On 12 Jan 2005 01:15:55 -0800, (e-mail address removed) wrote:

(snip horridly unindented code)
} catch (UnsupportedFlavorException ufe) {
} catch (IOException ioe) { }
}

(Begin to) find out what is happening..
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. ]
 
A

Andrew Thompson

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top