Returns the string contents of the JNLP clipboard, or from the system clipboard if we are not running under Java Web Start. - Java Native OS

Java examples for Native OS:Clipboard

Description

Returns the string contents of the JNLP clipboard, or from the system clipboard if we are not running under Java Web Start.

Demo Code


import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.jnlp.ClipboardService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;

public class Main{
    public static void main(String[] argv) throws Exception{
        System.out.println(getStringFromJwsOrSystemClipboard());
    }//  w  w w .j a va 2  s.c  o m
    /**
     * Returns the string contents of the JNLP clipboard, or from the system clipboard
     * if we are not running under Java Web Start.
     * 
     * @return clipboard string contents
     * @throws UnsupportedFlavorException
     * @throws IOException
     */
    public static String getStringFromJwsOrSystemClipboard()
            throws UnsupportedFlavorException, IOException {
        try {
            final ClipboardService cs = (ClipboardService) ServiceManager
                    .lookup(ClipboardService.class.getName());
            return (String) cs.getContents().getTransferData(
                    DataFlavor.stringFlavor);
        } catch (UnavailableServiceException e) {
            return (String) Toolkit.getDefaultToolkit()
                    .getSystemClipboard().getData(DataFlavor.stringFlavor);
        }

    }
}

Related Tutorials