Getting data from the computer clipboard in Java

Description

The following code shows how to getting data from the computer clipboard.

Example


// w  ww.j  a  v a 2 s.c om
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;

public class Main {
  public static void main(String[] args) throws Exception {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable tran = clipboard.getContents(null);

    if (tran != null && tran.isDataFlavorSupported(DataFlavor.stringFlavor)) {

      String clipboardContent = (String) tran.getTransferData(DataFlavor.stringFlavor);

      System.out.println(clipboardContent);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization