Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot get TransferHandler to work in Swing

 
Philip Persson
Greenhorn
Posts: 20
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the proper link: http://download.oracle.com/javase/tutorial/uiswing/dnd/toplevel.html
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and welcome to the Ranch Philip Persson
 
Philip Persson
Greenhorn
Posts: 20
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 20
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic