Retrieving Text from the System Clipboard - Java Native OS

Java examples for Native OS:Clipboard

Description

Retrieving Text from the System Clipboard

Demo Code

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws Exception {
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
        .getContents(null);// www  .  j a  v  a  2 s. co  m

    try {
      if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        String s = (String) t.getTransferData(DataFlavor.stringFlavor);
        System.out.println(s);
      }
    } catch (UnsupportedFlavorException e) {
    } catch (IOException e) {
    }
  }
}

Related Tutorials