I am having an issue trying to figure out how to retain formatting of text in a Java program when saving to the system clipboard.
It does not work with things such as Microsoft's Wordpad or Lotus Symphony. On the contrary, if I create a formatted string in Word and copy it, then it works properly with all the trial cases I try to paste it into.
I do not want to use any external sources such as org.eclipse.*.
Here are some links that I have compiled that might help me get pointed in the proper direction.
I feel as if I am not using the proper Data Flavor? http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/datatransfer/DataFlavor.html
I found this link which talks a lot about dataflavors, but does not shed much light on which one to use for formatted text. I do understand though that this might not be the same on every OS and I need to check to make sure it is supported on the OS I am using.
Thanks for all of your help in advanced, I really appreciate it!
Dan
EDIT
I am currently using some code from: http://lists.apple.com/archives/java-dev/2004/Jul/msg00359.html with a few small changes. The issue I am having currently, is I need to transmit the data to the clipboard in two different formats. "text/rtf" and "text/plain" seeing some programs do not support RTF. I am using inside clipboard to see what is inside the clipboard. I can successfully place either the RTF or the plain text, but not both simultaneously. When I do, only the last one gets added. Your help is greatly appreciated!
In summary, I cannot set the clipboard with two different data flavors at the same time.
import java.awt.datatransfer.*;
import java.io.*;
public class clipBoard
{
public static final String RTF_STRING = "{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 Courier;}}\r \n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nThis line is the default color\\line\r\n\\cf2\r\n\\tab This line is red and has a tab before it\\line\r\n\\cf1\r\n\\page This line is the default color and the first line on page 2\r\n}\r\n";
public static final DataFlavor RTF_FLAVOR = new DataFlavor("text/rtf", "Rich Formatted Text");
public static void main(String[] args){
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable t = new MyTransferable(
new ByteArrayInputStream(RTF_STRING.getBytes()), RTF_FLAVOR);
cb.setContents(t, null);
}
static class MyTransferable implements Transferable
{
private Object data = null;
private DataFlavor flavor;
public MyTransferable(Object o, DataFlavor df)
{ data = o; flavor = df; }
public Object getTransferData (DataFlavor df) throws
UnsupportedFlavorException, IOException
{
if (!flavor.isMimeTypeEqual(flavor))
throw new UnsupportedFlavorException(df);
return data;
}
public boolean isDataFlavorSupported (DataFlavor df)
{
return flavor.isMimeTypeEqual(df);
}
public DataFlavor[] getTransferDataFlavors()
{
DataFlavor[] ret = {flavor};
return ret;
}
}
}
After much searching around and trial and error and help from a friend Sebastian and Logan, it seems to be figured out. This allows multiple formats of data to be saved to the clip board at one time in Java while also retaining the styling and formatting of the text. Hopefully this helps someone. This was also a good resource. http://www.pindari.com/rtf1.html