import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import javax.swing.*; import java.io.*; public class JPanelCopy extends JFrame implements ClipboardOwner { private Clipboard clipboard; private JButton copy, paste; private JPanel copyMe; //Some tricks so we can replace the frames content in an inner class. final Container c = getContentPane(); public JPanelCopy() { // Obtain a reference to the system clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //Create the GUI. First place copy and paste buttons //in a panel in NORTH. copy = new JButton("Copy"); paste = new JButton("Paste"); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(copy); p.add(paste); copy.addActionListener (new CopyListener()); paste.addActionListener(new PasteListener()); c.add(p, BorderLayout.NORTH); //Then place the JPanel we shall copy in center. //It could contain anything, we'll place an image in it. copyMe = new JPanel(); copyMe.add(new JLabel(new ImageIcon("pernilla.jpg"))); c.add(copyMe, BorderLayout.CENTER); //Show the frame. setSize(400, 400); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } class CopyListener implements ActionListener { public void actionPerformed(ActionEvent event) { //This is were the string is copied to the system //clipboard. //Create a transferable object that contains the //string we are copying. JPanelSelection contents = new JPanelSelection(copyMe); // Place the transferable onto the clipboard. clipboard.setContents(contents, JPanelCopy.this); //Remove the JPanel. This //will illustrate that a panel actually is //inserted when we paste. c.remove(copyMe); repaint(); } } class PasteListener implements ActionListener { public void actionPerformed(ActionEvent event) { //This is were a string is read from the //system clipboard. //Get the content of the clipboard. Transferable contents = clipboard.getContents(this); System.out.println(contents); // Determine if data is available in JPanel flavor if(contents != null && contents.isDataFlavorSupported( JPanelSelection.JPanelFlavor)) { try { // Have contents cough up the JPanel // that was placed on the clipboard. JPanel p = (JPanel) contents.getTransferData( JPanelSelection.JPanelFlavor); //Display the new JPanel and revalidate so //it is displayed properly. c.add(p, BorderLayout.CENTER); repaint(); } catch(Exception e) { e.printStackTrace(); } } } } public void lostOwnership(Clipboard clip, Transferable transferable) { //We must keep the object we placed on the system clipboard //until this method is called. System.out.println("Lost ownership"); } public static void main(String[] args) { new JPanelCopy(); } } class JPanelSelection implements Transferable { //This class contains a JPanel, and can be placed on a clipboard. //Therefore it implements Transferable. //A static field that holds a reference to a DataFlavor that //represents a JPanel. The DataFlavor represents JPanel //as a java.lang.Class //object that represents a JPanel. static public DataFlavor JPanelFlavor = new DataFlavor(javax.swing.JPanel.class, "JPanel"); //This is where we store the panel beeing copied. private JPanel theJPanel; public JPanelSelection (JPanel p) { //Store the JPanel that is beeing copied. theJPanel = p; } public synchronized DataFlavor[] getTransferDataFlavors() { //This method comes from Transferable and shall return an //array of DataFlavors containing all the supported //DataFavors. We only support one. DataFlavor[] df = { JPanelFlavor }; return df; } /****************** isDataFlavorSupported ************/ public boolean isDataFlavorSupported(DataFlavor flavor) { //This method comes from Transferable and shall tell //wether the given DataFlavor is supported. return flavor.equals(JPanelFlavor); } public synchronized Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { //This method comes from Transferable and shall return our JPanel //in the given flavor. If it don't exist in that flavor we //are supposed to throw an exception. if (flavor.equals(JPanelFlavor)) return theJPanel; else throw new UnsupportedFlavorException(flavor); } }