Get the contents of the clipboard using java
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:
-
/*--------------------------------------------------------------------------
-
ClipboardV2 class
-
*****************
-
By: HappyFace http://www.engineeringserver.com
-
Contact: info [@] engineeringserver.com
-
Version: 01/August/2008
-
Last updated: 27/September/2008
-
"*****************
-
Note: Adapted from http://www.devx.com/Java/Article/22326 (original source)
-
//----------------------------------------------------------------------*/
-
import java.awt.Toolkit;
-
import java.awt.datatransfer.Clipboard;
-
import java.awt.datatransfer.DataFlavor;
-
import java.awt.datatransfer.Transferable;
-
import java.awt.datatransfer.UnsupportedFlavorException;
-
import java.io.IOException;
-
-
boolean newGrab = false;
-
String oldGrab = "";
-
ClipboardV2 CV2 = new ClipboardV2();
-
while(true){
-
CV2.run();
-
}
-
}
-
-
-
if (clipboardContents == null) {
-
return ("Clipboard is empty.");
-
}
-
else{
-
try {
-
return returnText;
-
}
-
ufe.printStackTrace();
-
ioe.printStackTrace();
-
}
-
return null;
-
}
-
}
-
-
public void run() {
-
try {
-
String newGrabbed = grabbed;
-
if(!oldGrab.equals(newGrabbed)){
-
oldGrab = newGrabbed;
-
}
-
e.printStackTrace();
-
}
-
}
-
}




