File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Cannot get TransferHandler to work in Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Cannot get TransferHandler to work in Swing" Watch "Cannot get TransferHandler to work in Swing" New topic
Author

Cannot get TransferHandler to work in Swing

Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

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:



I have created an object using that class as the type and have set the JTextArea to use that transferhandler:




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!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
The Swing tutorial on Drag and Drop and Data Transfer has examples for both of these functions.

I'm not sure how to combine it into one, but I guess you would need to get the data flavor to determine which code to execute.
Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

Rob Camick wrote:The Swing tutorial on Drag and Drop and Data Transfer has examples for both of these functions.

I'm not sure how to combine it into one, but I guess you would need to get the data flavor to determine which code to execute.


Thank you for replying, the link you provided seems to lead back to this thread. I have been through several tutorials and examples but.....am I correct in assuming that if the code was being executed, my println statements would be working? I am wondering if I am missing some additional link between the JTextArea and the new transferhandler class. It appears the Transferhandler code is not being executed.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Here is the proper link: http://download.oracle.com/javase/tutorial/uiswing/dnd/toplevel.html
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

Please BeForthrightWhenCrossPostingToOtherSites.
http://www.java-forums.org/awt-swing/38115-cannot-get-transferhandler-work-swing.html
http://www.javaprogrammingforums.com/whats-wrong-my-code/7123-cannot-get-transferhandler-work-swing.html


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
and welcome to the Ranch Philip Persson
Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20



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?
Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

Darryl Burke wrote:Please BeForthrightWhenCrossPostingToOtherSites.
l


I read your link about cross posting to other forums. I tend to approach every problem with great parallelism. I try a dozen different solutions, read a dozen different tutorials, visit two dozen different web sites for answers, and consult (hopefully) several dozen different experts. I didn't mean to break any rules.
Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

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>

Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot get TransferHandler to work in Swing