List of usage examples for javax.swing TransferHandler importData
public boolean importData(JComponent comp, Transferable t)
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 ww . j a v a 2s . co 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 va 2 s. co 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(); }