The framework defined by java.awt.dnd package implements drag-and-drop. To transfer any data, transferable API is the interface which has to be used by the class. Some classes even support multiple datatypes, which helps the developers to use in higher lever transfer protocols like clipboard, and drag and drop. The interface used here is java.awt.datatransfer.StringSelection. Some specifications should be followed while designing this interface such as:
When the data is moved from one application to other application, it should be compatible with the format of the new application. So, to overcome this problem, Data Flavors are used. The flavor depicts the information such that it can be used on any application. This information will be given a name and a class representing the class of object.
Multiple data transfer is not possible in a single transfer operation.
The interfaces to be used are java.awt.datatransfer.Clipboard and java.awt.datatransfer.ClipboardOwner.
The methods to be used are:
void setContents(Transferable content, ClipboardOwner owner)
Transferable getContents(Object requestor)
import java.awt.*; import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; public class Splesson extends Frame implements ClipboardOwner, ActionListener { TextArea srcText, dstText; Button copyButton, pasteButton; Clipboard clipboard = getToolkit().getSystemClipboard(); public Splesson() { super("Clipboard Test"); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); srcText = new TextArea(8, 32); c.gridwidth = 2; c.anchor = GridBagConstraints.CENTER; gridbag.setConstraints(srcText, c); add(srcText); copyButton = new Button("Copy Above"); copyButton.setActionCommand("copy"); copyButton.addActionListener(this); c.gridy = 1; c.gridwidth = 1; gridbag.setConstraints(copyButton, c); add(copyButton); pasteButton = new Button("Paste Below"); pasteButton.setActionCommand("paste"); pasteButton.addActionListener(this); pasteButton.setEnabled(false); c.gridx = 1; gridbag.setConstraints(pasteButton, c); add(pasteButton); dstText = new TextArea(8, 32); c.gridx = 0; c.gridy = 2; c.gridwidth = 2; gridbag.setConstraints(dstText, c); add(dstText); pack(); } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); if (cmd.equals("copy")) { // Implement Copy operation String srcData = srcText.getText(); if (srcData != null) { StringSelection contents = new StringSelection(srcData); clipboard.setContents(contents, this); pasteButton.setEnabled(true); } } else if (cmd.equals("paste")) { // Implement Paste operation Transferable content = clipboard.getContents(this); if (content != null) { try { String dstData = (String)content.getTransferData( DataFlavor.stringFlavor); dstText.append(dstData); } catch (Exception e) { System.out.println("Couldn't get contents in format: "+ DataFlavor.stringFlavor.getHumanPresentableName()); } } } } public void lostOwnership(Clipboard clipboard, Transferable contents) { System.out.println("Clipboard contents replaced"); } public static void main(String[] args) { Splesson test = new Splesson(); test.show(); } }
Output
Drag and drop operation requires a source from where the data should be dragged and a target to where the data dragged should be dropped. The two interfaces required are java.awt.dnd.DragSource and java.awt.dnd.DropTarget.