AWT - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

AWT Data Transfer

AWT Data Transfer

shape Introduction

Data Transfer is referred to a process of transferring the data. AWT Data Transfer explains the following concepts:
  • Description about Data Transfer
  • Clipboard API
  • Drag and Drop

shape Description

AWT DataTransfer can be done using java.awt.datatransfer package. It is the combination of classes & interfaces and helps in data transfer between the inter and intra applications. All these classes helps in transferring the data even if the copy-paste action is performed. 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: As said, java.awt.datatransfer.Transferable is the interface required to perform the data objects. 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.

shape Conceptual figure

Clipboard API

shape Description

Clipboard API is the higher level interface, which contains the operations like cut/copy/paste. While designing this interface, the following has to be considered:
  • Clipboard operations must be enabled to implement them in the native clipboard platform.
  • Creation of private clipboards must be enabled.
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)

shape Examples

[java] 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(); } }[/java] Output

Drag and Drop

shape Description

Another concept of AWT Data Transfer is Drag and Drop which is used for simple operations and makes the works easy. It helps in copying the files, new control creation and many more. The design specifications to be followed are:
  • It should not effect the performance.
  • Drag and Drop operation should be done between the Java application and native applications.
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.

Summary

shape Key Points

  • AWT Data Transfer requires Transferable interface.
  • StringSelection helps in multiple data transfers.
  • Clipboard API helps in copy/cut and paste the data onto the required application.
  • Drag requires a source and drop requires a target.