Java Reference
In-Depth Information
Table 19-1. Components with Built-in Support for Drag-and-Drop
Component
Drag
Drop
JColorChooser
X
X
JEditorPane
X
X
JFileChooser
X
JFormattedTextField
X
X
JList
X
JPasswordField
X
JTable
X
JTextArea
X
X
JTextField
X
X
JTextPane
X
X
JTree
X
Note For security reasons, you cannot drag text out of a JPasswordField component.
For the JColorChooser component, what you drag is a java.awt.Color object. The other
oddball of the bunch is the JFileChooser , where you literally drag a java.io.File object around
and drop it into a drop target. If the drop target doesn't support working with File objects, a
string representing the path is dropped instead.
As a simple demonstration, Listing 19-1 shows a program with two JColorChooser compo-
nents on a single screen. The setDragEnabled(true) call is made for both choosers, so you can
drag-and-drop colors between the two components with minimal coding effort.
Listing 19-1. Dragging-and-Dropping Colors Across JColorChooser Components
import javax.swing.*;
import java.awt.*;
public class DoubleColor {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Double Color Choosers");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JColorChooser left = new JColorChooser();
left.setDragEnabled(true);
frame.add(left, BorderLayout.WEST);
 
Search WWH ::




Custom Search