bordermisc focus    

Creating a GUI with JFC/Swing
Lesson: Using Other Swing Features
How to Use Drag and Drop and Data Transfer Most programs can benefit from the ability to transfer information, either between components, between Java applications, or between Java and native applications. The ability to transfer data takes two forms:

The TransferHandler object, the heart of the data transfer system, is described in more detail later. The arrows in the preceding diagrams show the path of the data.

Many Swing components provide out-of-the-box support for transferring data, as shown in the following table:

Data Transfer Support
Component Drag*
Copy
Drag*
Move
Drop Cut Copy Paste
JColorChooser**

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

 

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

     
JEditorPane

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

JFileChooser***

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

     

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

 
JFormattedTextField

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

JList

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

     

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

 
JPasswordField n/a n/a

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

n/a n/a

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

JTable

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

     

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

 
JTextArea

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

JTextField

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

JTextPane

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

JTree

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

     

uiswing/check 1212ALIGNNATURALSIZEFLAG3ALTchecked>

 
  *Enabled by invoking component.setDragEnabled(true) on the component.
 **Imports and exports data of type java.awt.Color.
***Exports both a list of filenames as java.io.File objects (preferred) and as strings for those components that do not accept File objects. The File Name text field in the file chooser accepts strings; the browser in the file chooser does not accept data. Note that as of release 1.4, clipboard copy from a JFileChooser is broken and actually causes the file to be moved when it is pasted. You may want to watch bug 4915992otherIcon (outside of the tutorial)


Version note: This section describes the drag and drop architecture implemented in release 1.4. Prior to 1.4, AWT support for data transfer was not well integrated into Swing components.

The data transfer mechanism is built in to every JComponent. For all the components with an empty space in the preceding table only a small amount of code is needed to customize the support. Support can easily be added to JComponents not listed in the table so they can fully participate in data transfer.

The rest of this section covers the following topics: