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 Main() {
    // Implement Copy operation
    StringSelection contents = new StringSelection("data");
    clipboard.setContents(contents, this);
    FlavorListener lis = new FlavorListener() {
        @Override//from   w ww . j  a  v  a2 s.co m
        public void flavorsChanged(FlavorEvent e) {
            System.out.println(e);
        }
    };
    clipboard.addFlavorListener(lis);
    clipboard.removeFlavorListener(lis);
    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    String dstData;
    try {
        dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
        System.out.println(dstData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public Main() {
    // Implement Copy operation
    StringSelection contents = new StringSelection("data");
    clipboard.setContents(contents, this);
    FlavorListener lis = new FlavorListener() {
        @Override/*from   w  w w  .ja  va2 s  . c o m*/
        public void flavorsChanged(FlavorEvent e) {
            System.out.println(e);
        }
    };
    clipboard.addFlavorListener(lis);
    FlavorListener[] flavors = clipboard.getFlavorListeners();
    for (FlavorListener f : flavors) {
        System.out.println(f);
    }

    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    String dstData;
    try {
        dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
        System.out.println(dstData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Clipboard.java

/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents./*  w w  w  .j a  v a2  s  .  com*/
*/
public void setClipboardContents(String aString) {
    StringSelection stringSelection = new StringSelection(aString);
    java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, this);
}

From source file:Main.java

public void dragGestureRecognized(DragGestureEvent evt) {
    Transferable t = new StringSelection("aString");
    dragSource.startDrag(evt, DragSource.DefaultCopyDrop, t, this);
}

From source file:ClipMe.java

public boolean action(Event e, Object o) {
    if (clipboard == null)
        clipboard = getToolkit().getSystemClipboard();
    if ((e.target == tf) || (e.target == copy)) {
        StringSelection data;//from w  w w  .ja  v  a 2s. c om
        data = new StringSelection(tf.getText());
        clipboard.setContents(data, data);
    } else if (e.target == paste) {
        Transferable clipData = clipboard.getContents(this);
        String s;
        try {
            s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
        } catch (Exception ee) {
            s = ee.toString();
        }
        ta.setText(s);
    }
    return true;
}

From source file:DragDropList.java

public void dragGestureRecognized(DragGestureEvent dge) {
    StringSelection transferable = new StringSelection(Integer.toString(list.getSelectedIndex()));
    ds.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
}

From source file:DragTest.java

public void dragGestureRecognized(DragGestureEvent dge) {
    System.out.println("Drag Gesture Recognized!");
    transferable = new StringSelection(jl.getSelectedValue().toString());
    ds.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
}

From source file:MainClass.java

public void actionPerformed(ActionEvent evt) {
    String cmd = evt.getActionCommand();

    if (cmd.equals("copy")) {
        // Implement Copy operation
        String srcData = srcText.getText();
        if (srcData != null) {
            StringSelection contents = new StringSelection(srcData);
            clipboard.setContents(contents, this);
            pasteButton.setEnabled(true);
        }//from  w  w  w  . j  a  v a 2 s  .  c  om
    } else if (cmd.equals("paste")) {
        // Implement Paste operation
        Transferable content = clipboard.getContents(this);
        if (content != null) {
            try {
                String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
                dstText.append(dstData);
            } catch (Exception e) {
                System.out.println("Couldn't get contents in format: "
                        + DataFlavor.stringFlavor.getHumanPresentableName());
            }
        }
    }
}

From source file:com.mirth.connect.client.ui.components.MirthTableTransferHandler.java

@Override
protected Transferable createTransferable(JComponent c) {
    TableModel model = ((JTable) c).getModel();
    int[] rows;/*from  w  ww.j a v  a2  s. c om*/
    if (c instanceof MirthTreeTable) {
        rows = ((MirthTreeTable) c).getSelectedModelRows();
    } else {
        rows = ((MirthTable) c).getSelectedModelRows();
    }

    // Don't put anything on the clipboard if no rows are selected
    if (rows.length == 0) {
        return null;
    }

    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < rows.length; i++) {
        builder.append(model.getValueAt(rows[i], primaryColumnIndex));

        String secondaryValue = String.valueOf(model.getValueAt(rows[i], secondaryColumnIndex));
        if (StringUtils.isNotBlank(secondaryValue)) {
            builder.append(" (");
            builder.append(model.getValueAt(rows[i], secondaryColumnIndex));
            builder.append(")");
        }

        if (i != rows.length - 1) {
            builder.append("\n");
        }
    }

    return new StringSelection(builder.toString());
}

From source file:net.sf.jabref.gui.ClipBoardManager.java

/**
 * Place a String on the clipboard, and make this class the
 * owner of the Clipboard's contents./*  w w w.java  2 s  .c om*/
 */
public void setClipboardContents(String aString) {
    StringSelection stringSelection = new StringSelection(aString);
    CLIPBOARD.setContents(stringSelection, this);
}