Sei sulla pagina 1di 4

NEWS

How to drag and drop with Java 2, Part 2


Create a library of drag and drop-enabled Swing components

By Gene De Lisa
JavaWorld | Aug 20, 1999 1:00 AM PT

Page 3 of 3

Dialogs
Let's say that the user has just dropped a duplicate item onto a , and you would like to ask if he or she really would like two
copies of that item in the list. You cannot show a dialog that requires input during a D&D operation (that is, in one of the listener's
callbacks) without the risk of deadlock. You might be able to get away with displaying a message dialog that is shown via
 without deadlock. However, if you would like the  to show, say, a confirm dialog, you need to
use the  class. This isn't a standard class in the JDK, but you can download it from the Swing Connection (see Resources).
The second example () of using the  class requires only a little modification to work with D&D. You create
an instance of a  in the method that is called when the data is dropped. The  method
can be modified for the dialog of your choice. The  method needs to return a boolean value based on the input from the
dialog. This boolean value may be retrieved in the 's finished method by the method. If the value is , go ahead
and add the item; otherwise, do nothing. If it's a local operation, the user is rearranging items and a duplicate already exists. In that
case, you should not show the dialog.

Deficiencies
There are some problems in the JDK that you should be aware of when using D&D. This section will identify them and present known
workarounds.

Caret problem
Text components work well as drop sites. You can't drag from them, however, since the drag gesture interferes with the caret.

Clipboard

Sign In | Register

The  class currently uses  instead of checking the MIME type. A custom text
flavor works well with D&D, but not with the clipboard.

Cursor feedback
Setting a custom cursor during  or  causes it to flicker between the default platform cursor and your custom cursor.
One workaround is to set the cursor to null and then set it to your custom cursor.

JTabbedPane
There is a problem with dragging and dropping children of a JTabbedPane. It seems to work only on the first tab. Actually, it will
return a child from the first tab even if another tab is selected. The solution is to create a subclass of JTabbbedPane to override the
 method inherited from . You can simply add a check to the existing code to see if the
container (current tab) is visible.

CardLayout
D&D doesn't work with the CardLayout manager. There is no known workaround at this time.

Conclusion
Even though there are several problems using D&D with Swing components, we have found solutions or workarounds for many of
them. In addition, rather than rewriting the same D&D code over and over again, we have developed a library of adapters and
abstract classes that can be used and/or subclassed for each new D&D-enabled component.

Gene De Lisa is a senior consultant/instructor and curriculum developer for Rockhopper Technologies Inc. He has been developing
and teaching Java since the fall of 1995. Since the beginning of 1998 his teaching and development has centered around Enterprise

JavaBeans, and he has most recently focused on J2EE. His original drag and drop development was for EJB clients. He recently
delivered a technical session on drag and drop at the 1999 JavaOne conference. He is also an amateur spheniscologist. You can see
him turn into his favorite spheniscid species in an Applet on his Web page. (He's the one with the tie.)

Learn more about this topic


"How to Drag and Drop with Java 2, Part 1," Gene De Lisa (JavaWorld, March 1999)
http://www.javaworld.com/javaworld/jw-03-1999/jw-03-dragndrop.html (http://www.javaworld.com/javaworld/jw-03-1999/jw-03dragndrop.html)
"Building a bigger sandbox," Todd M. Greanier (JavaWorld, August 1998). A good source for more information about security and creating
policy files
http://www.javaworld.com/javaworld/jw-08-1998/jw-08-sandbox.html (http://www.javaworld.com/javaworld/jw-08-1998/jw-08sandbox.html)
"Using a Swing Worker Thread," Hans Muller and Kathy Walrath (The Swing Connection Archive). Provides more information on using threads
with Swing components. You can also download the  source code here
http://java.sun.com/products/jfc/tsc/archive/tech_topics_arch/swing_worker/swing_worker.html
(http://java.sun.com/products/jfc/tsc/archive/tech_topics_arch/swing_worker/swing_worker.html)
The ever-growing Java drag and drop FAQ
http://www.rockhoppertech.com/java-drag-and-drop-faq.html (http://www.rockhoppertech.com/java-drag-and-drop-faq.html)
The Rockhopper D&D library (all the D&D-enabled components, models, and adapters discussed in this article are part of the free
Rockhopper D&D library)
http://www.rockhoppertech.com/download.html (http://www.rockhoppertech.com/download.html)
The slides from Gene's 1999 JavaOne D&D technical session
http://industry.java.sun.com/javaone/99/event/0,1768,617,00.html (http://industry.java.sun.com/javaone/99/event/0,1768,617,00.html)
The bug that prevents recognition of a 's 
http://developer.java.sun.com/developer/bugParade/bugs/4148450.html
(http://developer.java.sun.com/developer/bugParade/bugs/4148450.html)

Follow everything from JavaWorld

PREVIOUS

View Comments
Copyright 1994 - 2016 JavaWorld, Inc. All rights reserved.

Potrebbero piacerti anche