/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.*; 

2. import java.awt.* ; 

3. import java.applet.* ; 

4. import java.awt.event.*; 

5. import java.awt.datatransfer.*; 

6.  

7. public class ClipB 

8.     extends JApplet 

9.     implements  ClipboardOwner, ActionListener 

10.

11.  Clipboard clipboard ; 

12.  

13.   public void 

14.   actionPerformed( ActionEvent ev ) 

15.   { 

16.     if ( ev.getSource() == btfrom ) 

17.     { 

18.       Transferable content = clipboard.getContents(this); 

19.  

20.       if ( content != null

21.       { 

22.         try

23.         String data = 

24.         (String)content.getTransferData 

25.               ( DataFlavor.stringFlavor ); 

26.         ta.append( "\n" + data ); 

27.         } 

28.         catch ( Exception ex ) 

29.         { 

30.           ta.append ("Error : " + ex ); 

31.         } 

32.       } 

33.       return

34.     } 

35.  

36.     StringSelection newcontents = 

37.       new StringSelection 

38.         ( "Placed in clipboard by applet"  ) ; 

39.  

40.     clipboard.setContents ( newcontents, this ) ; 

41.   } 

42.  

43.   public void 

44.   init() { 

45.     cp = getContentPane(); 

46.     cp.setLayout( new FlowLayout() ); 

47.     cp.add ( btto ) ; 

48.     btto.addActionListener ( this ); 

49.     cp.add ( btfrom ) ; 

50.     btfrom.addActionListener ( this ); 

51.     cp.add ( sp ); 

52.  

53.     clipboard = 

54.     Toolkit.getDefaultToolkit().getSystemClipboard(); 

55.   } 

56.    public void 

57.    lostOwnership ( Clipboard clipboard, Transferable contents ) 

58.    { 

59.     ta.append ("\n===\nClipboard contents changed\n"); 

60.  

61.     DataFlavor[] dfar = contents.getTransferDataFlavors(); 

62.     for (int i=0; i < dfar.length; i++  ) 

63.     { 

64.       ta.append ("\n" + i +  " : " 

65.             + dfar[i].getHumanPresentableName()  ); 

66.       ta.append ("\n" + i +  " : " 

67.             + dfar[i].getMimeType()   ); 

68.     } 

69.    } 

70.  

71.  JButton btto = new JButton ( "TO clipboard" ); 

72.  JButton btfrom = new JButton ( "FROM clipboard" ); 

73.  JTextArea ta = new JTextArea ( 20,40 ); 

74.  JScrollPane sp = new JScrollPane(ta); 

75.  Container cp ; 

76.

77.