Obtains an object from the clipboard. - Java Native OS

Java examples for Native OS:Clipboard

Description

Obtains an object from the clipboard.

Demo Code

/*//from www  . jav  a 2  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/>.
 */
//package com.java2s;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;

public class Main {
    /**
     * Obtains an object from the clipboard.
     *
     * @param flavor   the type of object to obtain
     * @return      the obtained object, null if not available
     */
    public static Object pasteFromClipboard(DataFlavor flavor) {
        Clipboard clipboard;
        Object result;
        Transferable content;

        result = null;

        try {
            clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            content = clipboard.getContents(null);
            if ((content != null)
                    && (content.isDataFlavorSupported(flavor)))
                result = content.getTransferData(flavor);
        } catch (Exception e) {
            result = null;
        }

        return result;
    }
}

Related Tutorials