Example usage for javax.swing JLabel getTransferHandler

List of usage examples for javax.swing JLabel getTransferHandler

Introduction

In this page you can find the example usage for javax.swing JLabel getTransferHandler.

Prototype

public TransferHandler getTransferHandler() 

Source Link

Document

Gets the transferHandler property.

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  2s  . 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 w w  w.  jav  a2  s .c  o  m*/
    };
    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:ColorDrag.java

public static void main(String args[]) {
    // Create two JLabel objects
    final JLabel label1 = new JLabel("Drag here");
    JLabel label2 = new JLabel("Drop here");

    // Register TransferHandler objects on them: label1 transfers its
    // foreground color and label2 transfers its background color.
    label1.setTransferHandler(new TransferHandler("foreground"));
    label2.setTransferHandler(new TransferHandler("background"));

    // Give label1 a foreground color other than the default
    // Make label2 opaque so it displays its background color
    label1.setForeground(new Color(100, 100, 200));
    label2.setOpaque(true);//from  ww w . j  a va2s  . co  m

    // Now look for drag gestures over label1. When one occurs,
    // tell the TransferHandler to begin a drag.
    // Exercise: modify this gesture recognition so that the drag doesn't
    // begin until the mouse has moved 4 pixels. This helps to keep
    // drags distinct from sloppy clicks. To do this, you'll need both
    // a MouseListener and a MouseMotionListener.
    label1.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            TransferHandler handler = label1.getTransferHandler();
            handler.exportAsDrag(label1, e, TransferHandler.COPY);
        }
    });

    // Create a window, add the labels, and make it all visible.
    JFrame f = new JFrame("ColorDrag");
    f.getContentPane().setLayout(new FlowLayout());
    f.getContentPane().add(label1);
    f.getContentPane().add(label2);
    f.pack();
    f.setVisible(true);
}