get String Content from Clipboard - Java Native OS

Java examples for Native OS:Clipboard

Description

get String Content from Clipboard

Demo Code


//package com.java2s;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
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[] argv) throws Exception {
        System.out.println(getStringContent());
    }//from ww  w.ja v a  2 s.  c  om

    public static String getStringContent() {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable transfer = clip.getContents(null);
        if (transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            try {
                Object ob = transfer
                        .getTransferData(DataFlavor.stringFlavor);
                return ob.toString();
            } catch (UnsupportedFlavorException e) {
                return null;
            } catch (IOException e) {
                return null;
            }

        } else
            return null;
    }
}

Related Tutorials