copy a string into the Clipboard created - Java Native OS

Java examples for Native OS:Clipboard

Description

copy a string into the Clipboard created

Demo Code


//package com.java2s;
import java.awt.Toolkit;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;

public class Main {
    public static void main(String[] argv) throws Exception {
        String data = "java2s.com";
        copyString(data);//from w  ww.ja  v a  2s . c om
    }

    private static Clipboard clipboard = null;

    /**
     * Method to copy a string into the Clipboard
     * created : Nov 20, 2006 9:45:21 PM
     * @param String data : it is the String that is to be
     *                   copied into the Clip Board.
     */
    public static void copyString(String data) {
        copyTransferableObject(new StringSelection(data));
    }

    /**
     * Method to copy a Trasferable Object which is inturn
     * used by the copyString method to copy the string into
     * the Clip Board. It allows other type of objects also
     * to be copied into the Clip Board.
     * created : Nov 20, 2006 8:59:21 PM
     * @param Trasferable contents : an Object of Transferable Class.
     */
    private static void copyTransferableObject(Transferable contents) {
        getClipboard();
        clipboard.setContents(contents, null);
    }

    /**
     * Method to get the Clip Board contents, it initializes the
     * ClipBoard object. It starts a sub thread to copy the contents.
     * created : Nov 20, 2006 8:55:21 PM
     */
    private static void getClipboard() {
        // this is our simple thread that grabs the clipboard
        Thread clipThread = new Thread() {
            public void run() {
                clipboard = Toolkit.getDefaultToolkit()
                        .getSystemClipboard();
            }
        };

        // start the thread as a daemon thread and wait for it to die
        if (clipboard == null) {
            try {
                clipThread.setDaemon(true);
                clipThread.start();
                clipThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Related Tutorials