cant convert an object to a transferable object?

6

6e

Hi,

Im trying to implement the drag on drop example presented on
www.javaworld.com/javatips/jw-javatip97_p.html
in my own application.

The weird thing is that in my application I cannot get the object to
convert to a transferable object, while in the example it actually does
work...

It seems like such a silly problem, but Ive noticed on other forums
similar questions posted without answers. So I was wondering if one of
you could shed some insight into the situation.

The getUserObject() returns a defaultMutableTreeNode Object...

Object x = dragNode.getUserObject();
Transferable transferable = (Transferable) x;
 
T

Thomas Fritsch

6e said:
Im trying to implement the drag on drop example presented on
www.javaworld.com/javatips/jw-javatip97_p.html
in my own application.

The weird thing is that in my application I cannot get the object to
convert to a transferable object, while in the example it actually does
work...

It seems like such a silly problem, but Ive noticed on other forums
similar questions posted without answers. So I was wondering if one of
you could shed some insight into the situation.

The getUserObject() returns a defaultMutableTreeNode Object...

Object x = dragNode.getUserObject();
Transferable transferable = (Transferable) x;
I don't know what is the class of your object x, but probably it does
not implement the Transferable interface.
You would be lucky if x is a String. Then you can do
Transferable transferable = new StringSelection((String) x);
or
Transferable transferable = new StringSelection(x.toString());
(StringSelection is the Transferable-implementation for transferring a
String.) In all other cases you would have to develop your own
implementation of Transferable.
 
R

Roedy Green

Object x = dragNode.getUserObject();
Transferable transferable = (Transferable) x;

To experiment to see what is happening we need your complete code,
preferably pruned of all irrelevancies.

You could find out what getUserObject is giving you like this:

System.out.println( x.getClass() );

That class has to formally implement Transferable, not just have the
methods.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top