Example usage for java.awt.datatransfer Clipboard getContents

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

Introduction

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

Prototype

public synchronized Transferable getContents(Object requestor) 

Source Link

Document

Returns a transferable object representing the current contents of the clipboard.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable tran = clipboard.getContents(null);

    if (tran != null && tran.isDataFlavorSupported(DataFlavor.stringFlavor)) {

        String clipboardContent = (String) tran.getTransferData(DataFlavor.stringFlavor);

        System.out.println(clipboardContent);
    }//from   w  w  w  .j  a v  a  2  s.co m
}

From source file:MainClass.java

public static void main(String args[]) {
    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    System.out.println("Object Name: " + clip.getName());
    Transferable contents = clip.getContents(new MainClass().getClass());
    if (contents == null)
        System.out.println("\n\nThe clipboard is empty.");
    else {//  www  .j  a  v  a  2 s  .com
        DataFlavor flavors[] = contents.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; ++i) {
            System.out.println("\n\n Name: " + flavors[i].getHumanPresentableName());
            System.out.println("\n MIME Type: " + flavors[i].getMimeType());
            Class cl = flavors[i].getRepresentationClass();
            if (cl == null)
                System.out.println("null");
            else
                System.out.println(cl.getName());
        }
    }
}

From source file:Main.java

public static void main(String args[]) {
    String toClipboard = "Hello from Java!";
    StringSelection ss = new StringSelection(toClipboard);
    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    clip.setContents(ss, ss);/*from   w  w  w  .j a va  2 s  . co  m*/
    clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clip.getContents(new Main().getClass());
    if (contents == null) {
        System.out.println("The clipboard is empty.");
        return;
    }
    if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            String data = (String) contents.getTransferData(DataFlavor.stringFlavor);
            System.out.println(data);
        } catch (IOException ex) {
            System.out.println("IOException");
        } catch (UnsupportedFlavorException ex) {
            System.out.println("UnsupportedFlavorException");
        }
    } else {
        System.out.println("Wrong flavor.");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection testData = new StringSelection("Test Data");

    c.setContents(testData, testData);/*from   www  . j  av  a  2 s .c om*/
    // Get clipboard contents, as a String
    Transferable t = c.getContents(null);
    if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        Object o = t.getTransferData(DataFlavor.stringFlavor);
        String data = (String) t.getTransferData(DataFlavor.stringFlavor);
        System.out.println("Clipboard contents: " + data);
    }
    System.exit(0);
}

From source file:MainClass.java

public static void main(String args[]) {
    // void copyToClipboard() {
    String toClipboard = "Hello from Java!";
    StringSelection ss = new StringSelection(toClipboard);
    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    clip.setContents(ss, ss);/*from  w w w .j a va 2  s.co  m*/
    // Paste
    clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clip.getContents(new MainClass().getClass());
    if (contents == null)
        System.out.println("The clipboard is empty.");
    else {
        if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            try {
                String data = (String) contents.getTransferData(DataFlavor.stringFlavor);
                if (data == null)
                    System.out.println("null");
                else {
                    StringTokenizer st = new StringTokenizer(data, "\n");
                    while (st.hasMoreElements())
                        System.out.println(st.nextToken());
                }
            } catch (IOException ex) {
                System.out.println("IOException");
            } catch (UnsupportedFlavorException ex) {
                System.out.println("UnsupportedFlavorException");
            }
        } else
            System.out.println("Wrong flavor.");
    }

}

From source file:Main.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Clip Image");
    Container contentPane = frame.getContentPane();

    final Clipboard clipboard = frame.getToolkit().getSystemClipboard();

    Icon icon = new ImageIcon("jaeger.jpg");
    final JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());

    JScrollPane pane = new JScrollPane(label);
    contentPane.add(pane, BorderLayout.CENTER);

    JButton copy = new JButton("Copy");
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TransferHandler handler = label.getTransferHandler();
            handler.exportToClipboard(label, clipboard, TransferHandler.COPY);
        }/*  ww w. java2s .c  o m*/
    });

    JButton clear = new JButton("Clear");
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            label.setIcon(null);
        }
    });

    JButton paste = new JButton("Paste");
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Transferable clipData = clipboard.getContents(clipboard);
            if (clipData != null) {
                if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    TransferHandler handler = label.getTransferHandler();
                    handler.importData(label, clipData);
                }
            }
        }
    });

    JPanel p = new JPanel();
    p.add(copy);
    p.add(clear);
    p.add(paste);
    contentPane.add(p, BorderLayout.SOUTH);
    frame.setSize(300, 300);
    frame.show();
}

From source file:DragImage.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Clip Image");
    Container contentPane = frame.getContentPane();

    final Clipboard clipboard = frame.getToolkit().getSystemClipboard();

    Icon icon = new ImageIcon("jaeger.jpg");
    final JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());

    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent comp = (JComponent) e.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, e, TransferHandler.COPY);
        }//  w w w  .  ja v a 2 s.  co m
    };
    label.addMouseListener(mouseListener);

    JScrollPane pane = new JScrollPane(label);
    contentPane.add(pane, BorderLayout.CENTER);

    JButton copy = new JButton("Copy");
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TransferHandler handler = label.getTransferHandler();
            handler.exportToClipboard(label, clipboard, TransferHandler.COPY);
        }
    });

    JButton clear = new JButton("Clear");
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            label.setIcon(null);
        }
    });
    clear.setTransferHandler(new TransferHandler("text"));
    mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent comp = (JComponent) e.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, e, TransferHandler.COPY);
        }
    };
    clear.addMouseListener(mouseListener);

    JButton paste = new JButton("Paste");
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Transferable clipData = clipboard.getContents(clipboard);
            if (clipData != null) {
                if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    TransferHandler handler = label.getTransferHandler();
                    handler.importData(label, clipData);
                }
            }
        }
    });

    JPanel p = new JPanel();
    p.add(copy);
    p.add(clear);
    p.add(paste);
    contentPane.add(p, BorderLayout.SOUTH);

    JTextField tf = new JTextField();
    tf.setDragEnabled(true);
    contentPane.add(tf, BorderLayout.NORTH);
    frame.setSize(300, 300);
    frame.show();
}

From source file:Main.java

public static Transferable getContents(Clipboard clipboard) {
    try {/* w  w w .j a  va 2 s  . co m*/
        return clipboard.getContents(null);
    } catch (Throwable t) {
        return null;
    }
}

From source file:Main.java

public static String getClipboardData() {
    String result = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    Transferable contents = clipboard.getContents(null);
    DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
    DataFlavor dfTxt = DataFlavor.stringFlavor;

    boolean hasTransferableRTFText = (contents != null) && contents.isDataFlavorSupported(dfRTF);

    boolean hasTransferableTxtText = (contents != null) && contents.isDataFlavorSupported(dfTxt);

    if (hasTransferableRTFText) {
        try {//w  ww .  j a v  a2 s  .  c o m
            result = streamToString((InputStream) contents.getTransferData(dfRTF));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (hasTransferableTxtText) {
        try {
            result = (String) contents.getTransferData(dfTxt);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return result;
}

From source file:jo.alexa.sim.ui.logic.RuntimeLogic.java

public static void pasteSpec(RuntimeBean runtime)
        throws IOException, URISyntaxException, UnsupportedFlavorException, ParseException {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(null);
    if ((contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        String json = (String) contents.getTransferData(DataFlavor.stringFlavor);
        JSONObject jspec = (JSONObject) mParser.parse(json);
        AppSpecBean spec = FromJSONLogic.fromJSONAppSpec(jspec);
        selectMRU(runtime, spec);/*from  w  ww .  j  a v a2s  . c om*/
    }
}