Revision: 1740
Updated Code
at November 10, 2006 00:09 by whitetiger
Updated Code
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; public class ClipBoard extends JFrame implements ClipboardOwner, ActionListener { private static final long serialVersionUID = 1L; JTextArea srcText, dstText; JButton copyButton, pasteButton; Clipboard clipboard = getToolkit().getSystemClipboard(); public ClipBoard() { super("Clipboard Test"); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); srcText = new JTextArea(8, 32); c.gridwidth = 2; c.anchor = GridBagConstraints.CENTER; gridbag.setConstraints(srcText, c); add(srcText); copyButton = new JButton("Copy Above"); copyButton.setActionCommand("copy"); copyButton.addActionListener(this); c.gridy = 1; c.gridwidth = 1; gridbag.setConstraints(copyButton, c); add(copyButton); pasteButton = new JButton("Paste Below"); pasteButton.setActionCommand("paste"); pasteButton.addActionListener(this); pasteButton.setEnabled(false); c.gridx = 1; gridbag.setConstraints(pasteButton, c); add(pasteButton); dstText = new JTextArea(8, 32); c.gridx = 0; c.gridy = 2; c.gridwidth = 2; gridbag.setConstraints(dstText, c); add(dstText); pack(); } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); if(cmd.equals("copy")) { // Implement Copy operation String srcData = srcText.getText(); if(srcData != null) { StringSelection contents = new StringSelection(srcData); clipboard.setContents(contents, this); pasteButton.setEnabled(true); } } else if(cmd.equals("paste")) { // Implement Paste operation Transferable content = clipboard.getContents(this); if(content != null) { try { String dstData = (String) content.getTransferData(DataFlavor.stringFlavor); dstText.append(dstData); } catch(Exception e) { System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName()); } } } } public void lostOwnership(Clipboard clipboard, Transferable contents) { System.out.println("Clipboard contents replaced"); } public static void main(String[] args) { ClipBoard test = new ClipBoard(); test.setVisible(true); } }
Revision: 1739
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 9, 2006 23:54 by whitetiger
Initial Code
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; public class ClipBoard extends JFrame implements ClipboardOwner, ActionListener { private static final long serialVersionUID = 1L; JTextArea srcText, dstText; JButton copyButton, pasteButton; Clipboard clipboard = getToolkit().getSystemClipboard(); public ClipBoard() { super("Clipboard Test"); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); srcText = new JTextArea(8, 32); c.gridwidth = 2; c.anchor = GridBagConstraints.CENTER; gridbag.setConstraints(srcText, c); add(srcText); copyButton = new JButton("Copy Above"); copyButton.setActionCommand("copy"); copyButton.addActionListener(this); c.gridy = 1; c.gridwidth = 1; gridbag.setConstraints(copyButton, c); add(copyButton); pasteButton = new JButton("Paste Below"); pasteButton.setActionCommand("paste"); pasteButton.addActionListener(this); pasteButton.setEnabled(false); c.gridx = 1; gridbag.setConstraints(pasteButton, c); add(pasteButton); dstText = new JTextArea(8, 32); c.gridx = 0; c.gridy = 2; c.gridwidth = 2; gridbag.setConstraints(dstText, c); add(dstText); pack(); } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); if(cmd.equals("copy")) { // Implement Copy operation String srcData = srcText.getText(); if(srcData != null) { StringSelection contents = new StringSelection(srcData); clipboard.setContents(contents, this); pasteButton.setEnabled(true); } } else if(cmd.equals("paste")) { // Implement Paste operation Transferable content = clipboard.getContents(this); if(content != null) { try { String dstData = (String) content.getTransferData(DataFlavor.stringFlavor); dstText.append(dstData); } catch(Exception e) { System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName()); } } } } public void lostOwnership(Clipboard clipboard, Transferable contents) { System.out.println("Clipboard contents replaced"); } public static void main(String[] args) { ClipBoard test = new ClipBoard(); test.setVisible(true); } }
Initial URL
Initial Description
Initial Title
Java - CUT&PASTE
Initial Tags
email, phone, java, html, rails, ruby, text, web
Initial Language
Java