You are on page 1of 3

E:\Java\Java examples\DD2\CopyPasteExample.

java

Monday, November 11, 2013 2:12 PM

/* * A simple Java program that demonstrates how cut/copy/paste works. * * Ben Bederson, May 3, 2002 * * Bugs - Can't paste text from a webpage */ import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.dnd.*; import java.awt.datatransfer.*; import javax.swing.*; import javax.swing.text.*; public class CopyPasteExample extends JFrame { JMenuBar menuBar; JTextArea textArea; public CopyPasteExample() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a menu menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.add(new AbstractAction("Exit") { public void actionPerformed(ActionEvent e) { System.exit(0); } }); JMenu editMenu = new JMenu("Edit"); editMenu.add(new AbstractAction("Cut") { public void actionPerformed(ActionEvent e) { cut(); } }); editMenu.add(new AbstractAction("Copy") { public void actionPerformed(ActionEvent e) { copy(); } }); editMenu.add(new AbstractAction("Paste") { public void actionPerformed(ActionEvent e) { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable transferable = clipboard.getContents(null); paste(transferable); } }); menuBar.add(fileMenu); menuBar.add(editMenu); // Create a text box textArea = new JTextArea();
-1-

E:\Java\Java examples\DD2\CopyPasteExample.java

Monday, November 11, 2013 2:12 PM

textArea.setRows(20); textArea.setColumns(50); JScrollPane scrollPane = new JScrollPane(textArea); // Set up the GUI setJMenuBar(menuBar); getContentPane().add(scrollPane); pack(); // Calculate the size of the window setVisible(true); // Make the window visible } public void cut() { copy(); deleteSelection(); } public Transferable copy() { Transferable transferable = null; try { int start = textArea.getSelectionStart(); int end = textArea.getSelectionEnd(); transferable = new StringSelection(textArea.getText(start, end-start)); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(transferable, null); } catch (BadLocationException e) { System.out.println("Internal error selecting text"); } return transferable; } public void paste(Transferable transferable) { /* DataFlavor[] flavors = transferable.getTransferDataFlavors(); for (int i=0; i<flavors.length; i++) { System.out.println("flavor = " + flavors[i]); } */ // Warning - this is buggy for JDK1.3 (complains about not supporting unicode flavors) // but it works fine with JDK1.4 try { deleteSelection(); Object obj; try { Reader reader = DataFlavor.stringFlavor.getReaderForText(transferable); BufferedReader bufferedReader = new BufferedReader(reader); String str = ""; String temp; do {
-2-

E:\Java\Java examples\DD2\CopyPasteExample.java

Monday, November 11, 2013 2:12 PM

temp = bufferedReader.readLine(); if (temp != null) { str += temp + "\n"; } } while (temp != null); textArea.insert(str, textArea.getCaretPosition()); } catch (InvalidDnDOperationException e) { System.out.println("Can't paste with plain text flavor"); } } catch (UnsupportedFlavorException e) { System.out.println("Can't paste clipboard as text: " + e); } catch (IOException e) { System.out.println("Can't paste clipboard as text: " + e); } } protected void deleteSelection() { try { int start = textArea.getSelectionStart(); int end = textArea.getSelectionEnd(); if (end > start) { String temp = ""; if (start > 0) { temp = textArea.getText(0, start); } int len = textArea.getText().length(); temp += textArea.getText(end, len - end); int caret = textArea.getCaretPosition(); if ((caret > start) && (caret < end)) { caret = start; } else if (caret >= end) { caret -= (end - start); } textArea.setText(temp); textArea.setCaretPosition(caret); } } catch (BadLocationException e) { System.out.println("Internal error deleting selection"); } } public static void main(String[] args) { new CopyPasteExample(); } }

-3-