Java Utililty Methods Clipboard Image

List of utility methods to do Clipboard Image

Description

The list of methods to do Clipboard Image are organized into topic(s).

Method

booleancanPasteImageFromClipboard()
Checks whether a string can be obtained from the clipboard.
return canPasteFromClipboard(DataFlavor.imageFlavor);
ImagegetClipboardImage()
Gets an image from Clipboard.
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
    return (Image) clipboard.getData(DataFlavor.imageFlavor);
} catch (Exception e) {
    return null;
ImagegetClipboardImage()
Get the String residing on the clip board.
Image result = null;
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.imageFlavor);
if (hasTransferableText) {
    try {
        result = (Image) contents.getTransferData(DataFlavor.imageFlavor);
    } catch (UnsupportedFlavorException ex) {
...
ImagegetClipboardImage()
get Clipboard Image
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
if (cb.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
    try {
        return (Image) cb.getData(DataFlavor.imageFlavor);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
} else {
    return null;
StringgetFilenameFromClipboard(Clipboard clip)
Returns filename if clipboard contains it and the file exists, otherwise returns null
if (clip.isDataFlavorAvailable(DataFlavor.javaFileListFlavor)) {
    try {
        List files = (List) clip.getData(DataFlavor.javaFileListFlavor);
        if (files.size() == 1) {
            File file = (File) files.get(0);
            if (file.exists())
                return file.getAbsolutePath();
    } catch (Exception e) {
        e.printStackTrace();
if (clip.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
    try {
        String path = (String) clip.getData(DataFlavor.stringFlavor);
        if (path.length() < 512) {
            File file = new File(path);
            if (file.exists()) {
                return file.getAbsolutePath();
    } catch (Exception e) {
        e.printStackTrace();
return null;
booleanisImageDataFlavor(Clipboard clip)
test if data flavor has mime type x-java-image
DataFlavor fl[] = clip.getAvailableDataFlavors();
if (fl.length > 0) {
    DataFlavor f = fl[0];
    if ("image".equals(f.getPrimaryType())) {
        if (InputStream.class.getName().equals(f.getDefaultRepresentationClassAsString()))
            return true;
return false;
voidsetClipboardImage(final Image image)
set Clipboard Image
Transferable trans = new Transferable() {
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (isDataFlavorSupported(flavor)) {
            return image;
        throw new UnsupportedFlavorException(flavor);
    public DataFlavor[] getTransferDataFlavors() {
...
voidsetClipboardImage(final Image image)
Write the specified image to system clip board.
Transferable trans = new Transferable() {
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] { DataFlavor.imageFlavor };
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return DataFlavor.imageFlavor.equals(flavor);
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
...