Java Clipboard .getAvailableDataFlavors ()

Syntax

Clipboard.getAvailableDataFlavors() has the following syntax.

public DataFlavor [] getAvailableDataFlavors()

Example

In the following code shows how to use Clipboard.getAvailableDataFlavors() method.


/*from w ww .  j a va 2 s .c om*/
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class Main implements ClipboardOwner {
  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

  public Main() {
    // Implement Copy operation
    StringSelection contents = new StringSelection("data");
    clipboard.setContents(contents, this);
    
    DataFlavor[] fla = clipboard.getAvailableDataFlavors();
    for(DataFlavor f:fla){
      System.out.println(f);
    }
    
    
    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    String dstData;
    try {
      dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
      System.out.println(dstData);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void lostOwnership(Clipboard clipboard, Transferable contents) {
    System.out.println("Clipboard contents replaced");
  }

  public static void main(String[] args) {
    Main test = new Main();
  }
}




















Home »
  Java Tutorial »
    java.awt.datatransfer »




Clipboard
ClipboardOwner
DataFlavor
Transferable