An example illustrating drag and drop using JDK 1.4
Updated 15th Feb' 2002
|There is no copyright attached with the code available here. I have put this up to provide some information on issues I had a hard time resolving. The code has been put together as an example and in a hurry. You can use this code anyway you want at your own risk. Don't blame if weird things start happening to your machine. If you find any of this useful, drop me a mail :-). Please send suggestions / comments / flame to me|
This example shows how to
- Drag and drop using the new
TransferHandlerclass available with JDK 1.4. (
TransferHandlertakes care of installing key stroke handlers to capture ctrl-c/v/x to copy/paste/cut from your
- Drag and drop multiple elements from one
- Writing a custom
- Creating custom data flavors.
- Create a custom
Transferableobject to encapsulate underlying objects.
Issues still not addressed
- The annoying issue of having to click on an existing selection to be able to drag.
- Drag causes the visible selection to change.
- While dropping elements on the
JTableyou have to place the cursor on some existing row.
- Cut behaves like copy for now.
- Comments in code :-).
What kind of data transfer does this program handle?
Going through each component -
MyTreeaccepts data only from
MyTable. Data received from
MyTableaccepts data only from
MyTree. Data received from
MyTextaccepts data from
MyTable, or any other source which can provide data as text or in the
MyTextalso accepts file(s) dragged from Windows File explorer, these files appear as lines of file names in the text area.
MyTableis appended to existing data.
MyTreereplaces the existing data in the table.
- The only pre-requisite is that you should have JDK 1.4 installed.
- Download all the files listed above.
- Compile all of them (
javac *.javawill do just fine).
- To run, type