Java Reference
In-Depth Information
JColorChooser right = new JColorChooser();
right.setDragEnabled(true);
frame.add(right, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 19-1 shows this program in action, after dragging several colors. The drag area is the
Preview panel at the bottom. Dropping doesn't adjust the Recent list on the right side of each
color chooser, and you can drop across different color chooser panels.
Figure 19-1. Dueling JColorChoosers
TransferHandler Class
The magic of drag-and-drop happens because of the java.swing.TransferHandler class, intro-
duced with the J2SE 1.4 release. “What's magical about it?” you might ask. With this class, you
can pick which property you would like to be transferable in a drag-and-drop operation.
When you call setDragEnabled(true) to tell a component to support the drag gesture, the
component asks the installed TransferHandler what to transfer. If you don't like the default
object being transferred, you can call the setTransferHandler() method of the component,
passing in an appropriate replacement. You can also call setTransferHandler() when you want
to enable drag-and-drop support for a component that doesn't come with support built in.
The TransferHandler class has one public constructor:
public TransferHandler(String property)
Search WWH ::




Custom Search