Copies string to system clipboard. - Java Native OS

Java examples for Native OS:Clipboard

Description

Copies string to system clipboard.

Demo Code

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.
//package com.java2s;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.StringSelection;

import static java.awt.Toolkit.getDefaultToolkit;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "java2s.com";
        copyToClipboard(str);//  www.  j  a  v a2s .  c o  m
    }

    /**
     * Copies string to system clipboard.
     */
    public static void copyToClipboard(String str) {
        StringSelection copyItem = new StringSelection(str);
        Clipboard clipboard = getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(copyItem, null);
    }
}

Related Tutorials