Example usage for java.awt.datatransfer DataFlavor stringFlavor

List of usage examples for java.awt.datatransfer DataFlavor stringFlavor

Introduction

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

Prototype

DataFlavor stringFlavor

To view the source code for java.awt.datatransfer DataFlavor stringFlavor.

Click Source Link

Document

The DataFlavor representing a Java Unicode String class, where:
 representationClass = java.lang.String mimeType            = "application/x-java-serialized-object" 

Usage

From source file:Main.java

public static void main(String[] args) {

    DataFlavor df = DataFlavor.stringFlavor;

    System.out.println(df.isMimeTypeSerializedObject());
}

From source file:Main.java

public static void main(String[] args) {

    DataFlavor df = DataFlavor.stringFlavor;

    try {//from   w w  w.  jav a2 s.com
        System.out.println(df.clone());
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    DataFlavor df1 = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
    df1.selectBestTextFlavor(new DataFlavor[] { DataFlavor.stringFlavor });

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        String text = (String) t.getTransferData(DataFlavor.stringFlavor);
        System.out.println(text);
    }//from  w  w w  . j ava  2s. c  o  m

}

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);
    }/*  ww  w  . j av  a  2 s.  c  om*/
}

From source file:MainClass.java

public static void main(String[] args) {

    DataFlavor df = DataFlavor.stringFlavor;

    System.out.println("Mime type: " + df.getMimeType());
    System.out.println("Primary type: " + df.getPrimaryType());
    System.out.println("Subtype: " + df.getSubType());
    System.out.println("Name: " + df.getHumanPresentableName());
    String s = df.getRepresentationClass().toString();
    System.out.println("Representation class: " + s + "\n");

    df = DataFlavor.javaFileListFlavor;

    System.out.println("Mime type: " + df.getMimeType());
    System.out.println("Primary type: " + df.getPrimaryType());
    System.out.println("Subtype: " + df.getSubType());
    System.out.println("Name: " + df.getHumanPresentableName());
    s = df.getRepresentationClass().toString();
    System.out.println("Representation class: " + s);
}

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 .  j a  v  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);//  w w  w . java2  s .c om
    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 ww  .j  a  va  2 s. c  om*/
    // 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:DnDDemo3.java

public static void main(String[] args) {
    JPanel north = new JPanel();
    north.add(new JLabel("Drag from here:"));
    JTextField field = new JTextField(10);
    field.setDragEnabled(true);/*w  w  w.  j  av a  2s  .c o m*/
    north.add(field);

    final DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("first");
    listModel.addElement("second");
    final JList list = new JList(listModel);
    list.setDragEnabled(true);

    list.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            if (dl.getIndex() == -1) {
                return false;
            } else {
                return true;
            }
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }

            Transferable transferable = support.getTransferable();
            String data;
            try {
                data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            int index = dl.getIndex();
            if (dl.isInsert()) {
                listModel.add(index, data);
            } else {
                listModel.set(index, data);
            }
            Rectangle r = list.getCellBounds(index, index);
            list.scrollRectToVisible(r);
            return true;
        }
    });
    JScrollPane center = new JScrollPane();
    center.setViewportView(list);

    list.setDropMode(DropMode.USE_SELECTION);
    JPanel cp = new JPanel();
    cp.setLayout(new BorderLayout());
    cp.add(north, BorderLayout.NORTH);
    cp.add(center, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}