copy a Trasferable Object which is in turn used by the copyString method to copy the string into the Clip Board. - Java Native OS

Java examples for Native OS:Clipboard

Description

copy a Trasferable Object which is in turn used by the copyString method to copy the string into the Clip Board.

Demo Code


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

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.Transferable;

public class Main {
    private static Clipboard clipboard = null;

    /**//from   w w  w  . j  a v  a  2  s . c  o  m
     * 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