Example usage for java.awt.datatransfer StringSelection StringSelection

List of usage examples for java.awt.datatransfer StringSelection StringSelection

Introduction

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

Prototype

public StringSelection(String data) 

Source Link

Document

Creates a Transferable capable of transferring the specified String .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    StringSelection ss = new StringSelection("str");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}

From source file:Main.java

public static void main(String[] args) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    clipboard.setContents(new StringSelection("string"), null);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ClipboardOwner owner = new MyClipboardOwner();

    StringSelection ss = new StringSelection("A String");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, owner);
}

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  w  w  w.jav a  2  s.c  o  m
    // 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: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);// ww w .j a  v  a 2  s .c o 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: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 .ja v a  2  s  .c o 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 copyStringToClipboard(String string) {
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(string), null);
}

From source file:Main.java

public static void copyToSystemClipboard(String str) {
    StringSelection ss = new StringSelection(str);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}

From source file:Main.java

public static void setClipboardText(String str) {
    Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection selection = new StringSelection(str);
    clipBoard.setContents(selection, null);
}

From source file:Main.java

public static void copyTextToClipboard(String data) {
    StringSelection selection = new StringSelection(data);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
}