Copies the given transferable to the system's clipboard. - Java Native OS

Java examples for Native OS:Clipboard

Description

Copies the given transferable to the system's clipboard.

Demo Code

/*/*from w  w  w  . java2  s. c o  m*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JTable;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

public class Main{
    /**
     * Copies the given transferable to the system's clipboard.
     *
     * @param t      the transferable to copy
     */
    public static void copyToClipboard(Transferable t) {
        Toolkit.getDefaultToolkit().getSystemClipboard()
                .setContents(t, null);
    }
    /**
     * Copies the given string to the system's clipboard.
     *
     * @param s      the string to copy
     */
    public static void copyToClipboard(String s) {
        copyToClipboard(new TransferableString(s));
    }
    /**
     * Copies the given image to the system's clipboard.
     *
     * @param img      the image to copy
     */
    public static void copyToClipboard(BufferedImage img) {
        copyToClipboard(new TransferableImage(img));
    }
    /**
     * Copies the given JComponent as image to the system's clipboard.
     *
     * @param comp   the component to copy
     * @see      BufferedImage#TYPE_INT_RGB
     */
    public static void copyToClipboard(JComponent comp) {
        BufferedImage img;
        Graphics g;

        img = new BufferedImage(comp.getWidth(), comp.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        g = img.getGraphics();
        g.setPaintMode();
        g.fillRect(0, 0, comp.getWidth(), comp.getHeight());
        comp.printAll(g);
        copyToClipboard(img);
    }
    /**
     * Copies the given JTable as text to the system's clipboard.
     *
     * @param table   the table to copy
     */
    public static void copyToClipboard(JTable table) {
        Action copy;
        ActionEvent event;

        copy = table.getActionMap().get("copy");
        event = new ActionEvent(table, ActionEvent.ACTION_PERFORMED, "");
        copy.actionPerformed(event);
    }
}

Related Tutorials