Java Reference
In-Depth Information
CHAPTER 19
■ ■ ■
Drag-and-Drop Support
D rag-and-drop support allows a user to highlight something in one program or area of the
screen, select it, and relocate it to another program or area of the screen. For example, in
Microsoft Word, you can select a paragraph and drag it to a new location.
As Java has evolved, not only has the printing support changed with nearly every release,
but so has the drag-and-drop support. The last major change in drag-and-drop support happened
with the J2SE 1.4 release. Prior releases had support that was extremely difficult to use, especially to
get proper drag-and-drop behavior for complex (nontext) types. The JDK 5.0 version adds
some bug fixes and enhancements to drag-and-drop support.
You can implement drag-and-drop within your Java program in three ways:
For the components that come with built-in support, just enable it by calling their
setDragEnabled() method with an argument of true . Those components are JColorChooser ,
JFileChooser , JList , JTable , JTree , and all the subclasses of JTextComponent , except
JPasswordField .
For components without built-in support, you typically just need to configure the
TransferHandler for that component.
You can also go directly to the classes found in the java.awt.dnd package. Thanks to the
built-in support and configurability, this approach is rarely necessary.
Built-in Drag-and-Drop Support
Table 19-1 shows the components that offer built-in support for drag-and-drop. Initially, only
dropping is enabled for the components that support it, but after calling setDragEnabled(true)
for the component in question, you can enable dragging, too, if it's supported. The drag-and-
drop capabilities of the Java platform involve the underlying java.awt.datatransfer package
to move data around. Classes in this package allow you to describe the type of data to transfer.
729
Search WWH ::




Custom Search