Example usage for java.awt.datatransfer Transferable isDataFlavorSupported

List of usage examples for java.awt.datatransfer Transferable isDataFlavorSupported

Introduction

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

Prototype

public boolean isDataFlavorSupported(DataFlavor flavor);

Source Link

Document

Returns whether or not the specified data flavor is supported for this object.

Usage

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 ww  w  . jav 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: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 .jav  a 2 s  .  c  o  m
}

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);
    }// w w w  .  j  ava2 s  .co  m

}

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.imageFlavor)) {
        Image img = (Image) t.getTransferData(DataFlavor.imageFlavor);
    }/*from  www  .  ja  v  a 2s . c  om*/
}

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  . ja  v  a  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: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);/*  www  .j a  v a 2s  .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 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);
        }//w  ww.  jav a 2 s . 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 ww. j  a  va2s . c om*/
    };
    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

/**
 * Returns a list of files from a given transferable.
 *//* w  w w. ja  va2 s.  co m*/
public static List<File> getFiles(Transferable aTrans) {
    // Handle JavaFileListFlavor
    if (aTrans.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
        try {
            return (List) aTrans.getTransferData(DataFlavor.javaFileListFlavor);
        } catch (Exception e) {
            System.err.println(e);
            return null;
        }

    // Otherwise return null
    return null;
}

From source file:Main.java

public static String getClipboard() {
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    try {//from   ww  w . j  a  v a2s . c  om
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) t.getTransferData(DataFlavor.stringFlavor);

            return text.trim();
        }
    } catch (Exception e) {
    }
    return "";
}