Java - CUT&PASTE


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package system.clipboard;
  2.  
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.datatransfer.Clipboard;
  6. import java.awt.datatransfer.ClipboardOwner;
  7. import java.awt.datatransfer.DataFlavor;
  8. import java.awt.datatransfer.StringSelection;
  9. import java.awt.datatransfer.Transferable;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JTextArea;
  16.  
  17. public class ClipBoard extends JFrame implements ClipboardOwner, ActionListener
  18. {
  19. private static final long serialVersionUID = 1L;
  20.  
  21. JTextArea srcText, dstText;
  22. JButton copyButton, pasteButton;
  23. Clipboard clipboard = getToolkit().getSystemClipboard();
  24.  
  25. public ClipBoard()
  26. {
  27. super("Clipboard Test");
  28.  
  29. GridBagLayout gridbag = new GridBagLayout();
  30.  
  31. setLayout(gridbag);
  32.  
  33. srcText = new JTextArea(8, 32);
  34. c.gridwidth = 2;
  35. c.anchor = GridBagConstraints.CENTER;
  36. gridbag.setConstraints(srcText, c);
  37. add(srcText);
  38.  
  39. copyButton = new JButton("Copy Above");
  40. copyButton.setActionCommand("copy");
  41. copyButton.addActionListener(this);
  42. c.gridy = 1;
  43. c.gridwidth = 1;
  44. gridbag.setConstraints(copyButton, c);
  45. add(copyButton);
  46.  
  47. pasteButton = new JButton("Paste Below");
  48. pasteButton.setActionCommand("paste");
  49. pasteButton.addActionListener(this);
  50. pasteButton.setEnabled(false);
  51. c.gridx = 1;
  52. gridbag.setConstraints(pasteButton, c);
  53. add(pasteButton);
  54.  
  55. dstText = new JTextArea(8, 32);
  56. c.gridx = 0;
  57. c.gridy = 2;
  58. c.gridwidth = 2;
  59. gridbag.setConstraints(dstText, c);
  60. add(dstText);
  61.  
  62. pack();
  63. }
  64.  
  65. public void actionPerformed(ActionEvent evt)
  66. {
  67. String cmd = evt.getActionCommand();
  68.  
  69. if(cmd.equals("copy"))
  70. {
  71. // Implement Copy operation
  72. String srcData = srcText.getText();
  73.  
  74. if(srcData != null)
  75. {
  76. StringSelection contents = new StringSelection(srcData);
  77. clipboard.setContents(contents, this);
  78. pasteButton.setEnabled(true);
  79. }
  80. }
  81. else if(cmd.equals("paste"))
  82. {
  83. // Implement Paste operation
  84. Transferable content = clipboard.getContents(this);
  85. if(content != null)
  86. {
  87. try
  88. {
  89. String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
  90. dstText.append(dstData);
  91. }
  92. catch(Exception e)
  93. {
  94. System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName());
  95. }
  96. }
  97. }
  98. }
  99.  
  100. public void lostOwnership(Clipboard clipboard, Transferable contents)
  101. {
  102. System.out.println("Clipboard contents replaced");
  103. }
  104.  
  105. public static void main(String[] args)
  106. {
  107. ClipBoard test = new ClipBoard();
  108. test.setVisible(true);
  109. }
  110. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.