Using the clipboard : Clip Board « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » Clip BoardScreenshots 
Using the clipboard
Using the clipboard
 
// : c14:CutAndPaste.java
// Using the clipboard.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.awt.datatransfer.Clipboard;
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.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

public class CutAndPaste extends JFrame {
  private JMenuBar mb = new JMenuBar();

  private JMenu edit = new JMenu("Edit");

  private JMenuItem cut = new JMenuItem("Cut"), copy = new JMenuItem("Copy"),
      paste = new JMenuItem("Paste");

  private JTextArea text = new JTextArea(2020);

  private Clipboard clipbd = getToolkit().getSystemClipboard();

  public CutAndPaste() {
    cut.addActionListener(new CutL());
    copy.addActionListener(new CopyL());
    paste.addActionListener(new PasteL());
    edit.add(cut);
    edit.add(copy);
    edit.add(paste);
    mb.add(edit);
    setJMenuBar(mb);
    getContentPane().add(text);
  }

  class CopyL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String selection = text.getSelectedText();
      if (selection == null)
        return;
      StringSelection clipString = new StringSelection(selection);
      clipbd.setContents(clipString, clipString);
    }
  }

  class CutL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String selection = text.getSelectedText();
      if (selection == null)
        return;
      StringSelection clipString = new StringSelection(selection);
      clipbd.setContents(clipString, clipString);
      text.replaceRange("", text.getSelectionStart(), text
          .getSelectionEnd());
    }
  }

  class PasteL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Transferable clipData = clipbd.getContents(CutAndPaste.this);
      try {
        String clipString = (StringclipData
            .getTransferData(DataFlavor.stringFlavor);
        text.replaceRange(clipString, text.getSelectionStart(), text
            .getSelectionEnd());
      catch (Exception ex) {
        System.err.println("Not String flavor");
      }
    }
  }

  public static void main(String[] args) {
    run(new CutAndPaste()300200);
  }

  public static void run(JFrame frame, int width, int height) {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width, height);
    frame.setVisible(true);
  }
///:~



           
         
  
Related examples in the same category
1. Communicating with the System ClipboardCommunicating with the System Clipboard
2. Sending Image Objects through the ClipboardSending Image Objects through the Clipboard
3. Placing text on the computer clipboard
4. Getting data from the computer clipboard
5. Get string value from clipboard
6. Write a string to the system clipboard
7. Getting and Setting an Image on the System Clipboard
8. Setting an image on the clipboard with a custom Transferable object to hold the image
9. Determining When an Item Is No Longer on the System Clipboard
10. implements ClipboardOwner
11. Copying data to system clipboard
12. Taken from the Sun documentation on Clipboard APITaken from the Sun documentation on Clipboard API
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.