Example usage for java.awt.datatransfer Clipboard getAvailableDataFlavors

List of usage examples for java.awt.datatransfer Clipboard getAvailableDataFlavors

Introduction

In this page you can find the example usage for java.awt.datatransfer Clipboard getAvailableDataFlavors.

Prototype

public DataFlavor[] getAvailableDataFlavors() 

Source Link

Document

Returns an array of DataFlavor s in which the current contents of this clipboard can be provided.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.delay(40);/*from w  w w .java 2 s  . com*/
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.delay(404);

    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor[] flavors = cb.getAvailableDataFlavors();
    for (DataFlavor flavor : flavors) {
        if (flavor.toString().indexOf("java.awt.Image") <= 0) {
            continue;
        }
        Image i = (Image) cb.getData(flavor);
        BufferedImage bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(i, 0, 0, null);
        g.dispose();
        ImageIO.write(bi, "png", new File("c:/Java_Dev/test.png"));
    }
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * @return the plain text flavor from the clipboard
 */// w  ww . j a  v  a2s  . c om
public static String getTextFromClipboard() {
    Clipboard sysClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    for (DataFlavor flavor : sysClipboard.getAvailableDataFlavors()) {
        if (flavor.isMimeTypeEqual(DataFlavor.getTextPlainUnicodeFlavor())) {
            try {
                StringBuilder sb = new StringBuilder();
                Object dataObj = sysClipboard.getData(flavor);
                if (dataObj instanceof String) {
                    sb.append((String) dataObj);

                } else if (dataObj instanceof InputStreamReader) {
                    Reader reader = (InputStreamReader) sysClipboard.getData(flavor);
                    char[] buffer = new char[1024];
                    int len = reader.read(buffer);
                    sb.append(new String(buffer, 0, len));

                    while (len > -1) {
                        len = reader.read(buffer);
                        if (len > 0) {
                            sb.append(buffer);
                        }
                    }
                }

                if (sb.length() > 0) {
                    return sb.toString();
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }
            break;
        }
    }
    return null;
}