Java Clipboard Image setClipboardImage(final Image image)

Here you can find the source of setClipboardImage(final Image image)

Description

set Clipboard Image

License

Apache License

Declaration

public static void setClipboardImage(final Image image) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

import java.io.IOException;

public class Main {

    public static void setClipboardImage(final Image image) {
        Transferable trans = new Transferable() {
            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
                if (isDataFlavorSupported(flavor)) {
                    return image;
                }//from  ww w  .j  ava  2 s.c om
                throw new UnsupportedFlavorException(flavor);
            }

            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[] { DataFlavor.imageFlavor };
            }

            public boolean isDataFlavorSupported(DataFlavor flavor) {
                return DataFlavor.imageFlavor.equals(flavor);
            }
        };

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
    }

    public static void setClipboardImage(String content) {
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(content), null);
    }
}

Related

  1. getClipboardImage()
  2. getClipboardImage()
  3. getClipboardImage()
  4. getFilenameFromClipboard(Clipboard clip)
  5. isImageDataFlavor(Clipboard clip)
  6. setClipboardImage(final Image image)