Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Use Java to Interact with Your Clipboard : Page 4

These days end users expect to use the ubiquitous "clipboard" concept as a transfer station for data, and if your applications don't support it, users won't be pleased. Learn how to use the java.awt.datatransfer package to cut, copy, and paste to a clipboard in Java.


WEBINAR: On-demand Event

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

Writing to Text to the Clipboard
The class TextWriter (contained in the sample code) shown below demonstrates how to write text to the clipboard:

public class TextWriter { public static void writeToClipboard(String writeMe) { // get the system clipboard Clipboard systemClipboard = Toolkit .getDefaultToolkit() .getSystemClipboard(); // set the textual content on the clipboard to our // Transferable object // we use the Transferable transferableText = new StringSelection(writeMe); systemClipboard.setContents( transferableText, null); } public static void main(String[] args) { System.out.println( "Writing text to the system clipboard."); String writeMe = "I'd like to be put on the clipboard"; writeToClipboard(writeMe); } }

Get the system Clipboard object and write to it using the setContents method. This method requires you to provide the payload of what you want to write as a Transferable object. Since you aren't allowed to instantiate the interface, use the StringSelection implementation which implements the Transferable interface.

Figure 1. Testing Proper Function Execution: Use Notepad to ensure the TextWriter program properly wrote text to the system clipboard.

After executing the TextWriter class, you can execute the TextGrabber class to make sure text was written to the Clipboard. For a more convincing test of proper function execution, open up Notepad and use the Paste (Ctrl+V) function to make sure the text found its way to its clipboard destination. Figure 1 demonstrates:

What About Images and the Clipboard?
Java 1.4 also introduced the java.awt.datatransfer.DataFlavor.imageFlavor predefined Flavor. As with java.awt.datatransfer.DataFlavor.stringFlavor, having a Flavor predefined makes life pretty straightforward. imageFlavor represents the Java Image class: java.awt.Image. The MIME type associated with this flavor is "image/x-java-image." The imageFlavor Flavor allows you to interact with common image formats like GIF and JPEG on the clipboard.

The sample code [link] for this article provides a class named Viewer. This class simply takes a java.awt.Image object and draws the image to the screen. The code for the Viewer class is shown below, as you can see, there is nothing really out of the ordinary with the java.awt code. I do use a MediaTracker to make sure my image is loaded before attempting to display it.

import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Viewer extends Frame { private Image image; // method takes in a java.awt.Image object // and displays it public Viewer(Image viewMe) { image = viewMe; // use a MediaTracker to make sure object // is loaded before trying to display it MediaTracker mediaTracker = new MediaTracker(this); mediaTracker.addImage(image, 0); try { mediaTracker.waitForID(0); } catch (InterruptedException ie) { System.err.println(ie); System.exit(1); } System.out.println("Finished loading image object"); // add a Window Listener to handle closing of a window addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // set size of display window setSize(image.getWidth(null), image.getHeight(null)); // set title of display window setTitle("Viewing Image from Clipboard"); // show image show(); } public void paint(Graphics graphics) { graphics.drawImage(image, 0,0, null); } }

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date