Java Reference
In-Depth Information
The createTransferable() method returns a reference to the Transferable implementation.
When the clipboard paste operation is executed, or the drop gesture is performed while dragging,
the Transferable object will be notified to get the object to transfer.
public Transferable createTransferable(JComponent comp) {
// Clear
image = null;
if (comp instanceof JLabel) {
JLabel label = (JLabel)comp;
Icon icon = label.getIcon();
if (icon instanceof ImageIcon) {
image = ((ImageIcon)icon).getImage();
return this;
}
} else if (comp instanceof AbstractButton) {
AbstractButton button = (AbstractButton)comp;
Icon icon = button.getIcon();
if (icon instanceof ImageIcon) {
image = ((ImageIcon)icon).getImage();
return this;
}
}
return null;
}
The importData() method is called when data is dropped into the component or pasted
from the clipboard. It has two parameters: a JComponent to paste the clipboard data and the
clipboard data via a Transferable object. Assuming the method receives a format supported by
the Java platform, the component associated with the transfer handler gets a new image to display.
public boolean importData(JComponent comp, Transferable t) {
if (comp instanceof JLabel) {
JLabel label = (JLabel)comp;
if (t.isDataFlavorSupported(flavors[0])) {
try {
image = (Image)t.getTransferData(flavors[0]);
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
return true;
} catch (UnsupportedFlavorException ignored) {
} catch (IOException ignored) {
}
}
} else if (comp instanceof AbstractButton) {
AbstractButton button = (AbstractButton)comp;
Search WWH ::




Custom Search