Engineeringserver.com

A community for computer science students & developers about software development, game development, game design, games, anime preview & reviews and more!


Get the contents of the clipboard using java

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
365 views

So, i was looking for a way to grab content from a clipboard and googled a bit about it. After reading a page on the Devx site i rewrote the code a bit and the result can be seen below. This version only grabs text but i'll write another demo that grabs text and images as well when i have the time for it.

JAVA:
  1. /*--------------------------------------------------------------------------
  2. ClipboardV2 class
  3. *****************
  4. By: HappyFace   http://www.engineeringserver.com
  5. Contact:        info [@] engineeringserver.com
  6. Version:        01/August/2008
  7. Last updated:   27/September/2008
  8. "*****************
  9. Note: Adapted from http://www.devx.com/Java/Article/22326 (original source)
  10. //----------------------------------------------------------------------*/
  11. import java.awt.Toolkit;
  12. import java.awt.datatransfer.Clipboard;
  13. import java.awt.datatransfer.DataFlavor;
  14. import java.awt.datatransfer.Transferable;
  15. import java.awt.datatransfer.UnsupportedFlavorException;
  16. import java.io.IOException;
  17.  
  18. public class ClipboardV2 implements Runnable{
  19. boolean newGrab = false;
  20. String oldGrab = "";
  21. public static void main(String[] args){
  22. ClipboardV2 CV2 = new ClipboardV2();
  23. while(true){
  24. CV2.run();
  25. }
  26. }
  27.  
  28. public static String getClipboard() {
  29. Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  30. Transferable clipboardContents =systemClipboard.getContents(null);
  31.  
  32. if (clipboardContents == null) {
  33. return ("Clipboard is empty.");
  34. }
  35. else{
  36. try {
  37. if (clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
  38. String returnText = (String) clipboardContents.getTransferData(DataFlavor.stringFlavor);
  39. return returnText;
  40. }
  41. ufe.printStackTrace();
  42. } catch (IOException ioe) {
  43. ioe.printStackTrace();
  44. }
  45. return null;
  46. }
  47. }
  48.  
  49. public void run() {
  50. try {
  51. Thread.sleep(100);
  52. String grabbed = getClipboard();
  53. String newGrabbed = grabbed;
  54. if(!oldGrab.equals(newGrabbed)){
  55. oldGrab = newGrabbed;
  56. System.out.println("Grabbed: " + grabbed);
  57. }
  58. } catch (InterruptedException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }


Your Ad Here

Read a textfile into an ArrayList

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
514 views
JAVA:
  1. /*--------------------------------------------------------------------------
  2. ScanToArrayDemo class
  3. *****************
  4. By: HappyFace   http://www.engineeringserver.com
  5. Contact:        info [@] engineeringserver.com
  6. Version:        10/September/2008
  7. "*****************
  8. Note: Read a textfile into an ArrayList
  9. //----------------------------------------------------------------------*/
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Scanner;
  15.  
  16. public class ScanToArrayDemo {
  17. public static void main(String[] args){
  18. File fInput     = new File("c:\\input.txt");
  19. ArrayList textHolder = new ArrayList();
  20. String tmp = "";
  21. try {
  22. Scanner scInput = new Scanner(fInput);
  23. while (scInput.hasNextLine()){
  24. tmp = scInput.nextLine();
  25. textHolder.add(tmp);
  26. }
  27. }
  28. e.printStackTrace();
  29. }
  30. catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. //display the content of the array
  34. System.out.println(textHolder.get(3));
  35. }
  36. }


Your Ad Here