get String Clipboard - Java Native OS

Java examples for Native OS:Clipboard

Description

get String Clipboard

Demo Code


//package com.java2s;

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[] argv) throws Exception {
        System.out.println(getStringClipboard());
    }//from  w  w w  . ja va2s.com

    public static String getStringClipboard()
            throws UnsupportedFlavorException, IOException {
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
                .getContents(null);
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) t
                    .getTransferData(DataFlavor.stringFlavor);
            return text;
        }
        return null;
    }
}

Related Tutorials