/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package system.clipboard; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; { private static final long serialVersionUID = 1L; JTextArea srcText, dstText; JButton copyButton, pasteButton; public ClipBoard() { super("Clipboard Test"); setLayout(gridbag); c.gridwidth = 2; gridbag.setConstraints(srcText, c); add(srcText); copyButton.setActionCommand("copy"); copyButton.addActionListener(this); c.gridy = 1; c.gridwidth = 1; gridbag.setConstraints(copyButton, c); add(copyButton); pasteButton.setActionCommand("paste"); pasteButton.addActionListener(this); pasteButton.setEnabled(false); c.gridx = 1; gridbag.setConstraints(pasteButton, c); add(pasteButton); c.gridx = 0; c.gridy = 2; c.gridwidth = 2; gridbag.setConstraints(dstText, c); add(dstText); pack(); } { if(cmd.equals("copy")) { // Implement Copy operation if(srcData != null) { clipboard.setContents(contents, this); pasteButton.setEnabled(true); } } else if(cmd.equals("paste")) { // Implement Paste operation if(content != null) { try { dstText.append(dstData); } { System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName()); } } } } { } { ClipBoard test = new ClipBoard(); test.setVisible(true); } }