Example usage for java.awt.datatransfer DataFlavor imageFlavor

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

Introduction

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

Prototype

DataFlavor imageFlavor

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

Click Source Link

Document

The DataFlavor representing a Java Image class, where:
 representationClass = java.awt.Image mimeType            = "image/x-java-image" 
Will be null if java.awt.Image is not visible, the java.desktop module is not loaded, or the java.desktop module is not in the run-time image.

Usage

From source file:Main.java

public static void main(String[] args) {

    DataFlavor df = DataFlavor.imageFlavor;

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

From source file:Main.java

public static void main(String[] args) {

    DataFlavor df = DataFlavor.stringFlavor;
    System.out.println(df.equals(DataFlavor.imageFlavor));
}

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  w  w  w  .  java 2 s.  c  om*/
}

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);
        }/*from   www. ja v a2s.c om*/
    });

    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);
        }//from   w  w w. ja va2  s  .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

public DataFlavor[] getTransferDataFlavors() {
    return new DataFlavor[] { DataFlavor.imageFlavor };
}

From source file:Main.java

public boolean isDataFlavorSupported(DataFlavor flavor) {
    return DataFlavor.imageFlavor.equals(flavor);
}

From source file:Main.java

public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    if (!DataFlavor.imageFlavor.equals(flavor)) {
        throw new UnsupportedFlavorException(flavor);
    }/* w  w  w.ja  v a2s .co  m*/
    return image;
}

From source file:ImageTransferTest.java

/**
 * Pastes the image from the system clipboard into the image label.
 *//*  ww  w.j  a  v  a 2  s .  c o  m*/
private void paste() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor flavor = DataFlavor.imageFlavor;
    if (clipboard.isDataFlavorAvailable(flavor)) {
        try {
            image = (Image) clipboard.getData(flavor);
            label.setIcon(new ImageIcon(image));
        } catch (UnsupportedFlavorException exception) {
            JOptionPane.showMessageDialog(this, exception);
        } catch (IOException exception) {
            JOptionPane.showMessageDialog(this, exception);
        }
    }
}

From source file:ImageTransferTest.java

public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
    if (flavor.equals(DataFlavor.imageFlavor)) {
        return theImage;
    } else {//  ww w .j av a  2  s.co  m
        throw new UnsupportedFlavorException(flavor);
    }
}