Browse DevX
Sign up for e-mail newsletters from DevX


Use Java to Interact with Your Clipboard : Page 6

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Copying an Image to the Clipboard
This process is exemplified in the class ImageWriter. The ImageWriter class reads in an image file from disk and copies it to the clipboard. The interesting work of this class is performed by a couple of static methods and an inner class.

The static method grabImageFromFile does what its name implies—it takes in a file name and returns an Image:

public static Image grabImageFromFile(String fileName) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage(fileName); System.out.println("Done loading"); return image; }

The filename is received from standard input in the main method of the ImageWriter class; the sample code allows you to type in the location of the image file you would like to copy to the clipboard. Remember to use \\ instead of \, as the program expects a Java-friendly file path.

The next major method of the ImageGrabber class is the static method setClipboard:

public static void setClipboard(Image image) { ImageSelection imageSelection = new ImageSelection(image); Toolkit .getDefaultToolkit() .getSystemClipboard() .setContents(imageSelection, null); }

The above method simply takes an java.awt.Image object and copies it to the system clipboard. This is done via the setContents method of the Clipboard object representing the system clipboard. The setContents method expects a Transferable object as its first argument to transfer the data payload. The code uses an ImageSelection as the argument. But what is this ImageSelection object?

I created the ImageSelection class myself as an inner class:

// Inner class is used to hold an image while on the clipboard. public static class ImageSelection implements Transferable { // the Image object which will be housed by the ImageSelection private Image image; public ImageSelection(Image image) { this.image = image; } // Returns the supported flavors of our implementation public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[] {DataFlavor.imageFlavor}; } // Returns true if flavor is supported public boolean isDataFlavorSupported(DataFlavor flavor) { return DataFlavor.imageFlavor.equals(flavor); } // Returns Image object housed by Transferable object public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException,IOException { if (!DataFlavor.imageFlavor.equals(flavor)) { throw new UnsupportedFlavorException(flavor); } // else return the payload return image; } }

Earlier in this article, I discussed the Transferable interface. When dealing with text, you were spoon-fed with the java.awt.datatransfer.StringSelection implementation of the java.awt.datatransfer.Transferable interface. With images, the Java language does not provide an innate Transferable implementation that supports images. Thus, you need to create one. As stated earlier, to implement the Transferable interface, you are mandated to implement the getDataTransferDataFlavors() method to return an array of DataFlavor types you support. In this case, I returned an array of size one which simply included the DataFlavor.imageFlavor Flavor. Next, defined the method: DataFlavor object is supported by the Transferable implementation. I am only interested in incoming arguments of the imageFlavor Flavor, so that's the only time I'll return true. Lastly, I implemented the getTransferData method. It is here that if the incoming DataFlavor is, in fact, the imageFlavor Flavor, I return the private java.awt.Image object (i.e., my payload) housed in the Transferable object.

To test the ImageGrabber class, execute the class and you will be prompted to provide the path to an image file. Go ahead and provide the DOS file location to a GIF or JPEG file. After execution, if things have gone according to plan, you should be able to open up your favorite image editing program and see that the image you specified is available for pasting (i.e., it was added to the keyboardclipboard). You can also use the ImageGrabber class introduced earlier prove to yourself that the image you pointed to was written to the clipboard.

Kulvir Singh Bhogal works as an IBM consultant, devising and implementing J2EE-centric solutions at customer sites across the nation.
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