Example usage for javax.swing TransferHandler exportToClipboard

List of usage examples for javax.swing TransferHandler exportToClipboard

Introduction

In this page you can find the example usage for javax.swing TransferHandler exportToClipboard.

Prototype

public void exportToClipboard(JComponent comp, Clipboard clip, int action) throws IllegalStateException 

Source Link

Document

Causes a transfer from the given component to the given clipboard.

Usage

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   w w  w .j  a v 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);
        }//from  www . j  a  v a  2  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:org.schreibubi.JCombinations.ui.GridChartPanel.java

/**
 * Copy to clipboard/* ww  w .j  a  v a  2  s .c  om*/
 */
public void copyToClipboard() {
    TransferHandler t = getTransferHandler();
    if (t != null)
        t.exportToClipboard(this, Toolkit.getDefaultToolkit().getSystemClipboard(), TransferHandler.COPY);
}