Formatted cliboard Java

2020-03-26 10:59发布

问题:

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.

http://www.javaworld.com/cgi-bin/mailto/x_java.cgi?pagetosend=/export/home/httpd/javaworld/javaworld/javatips/jw-javatip61.html&pagename=/javaworld/javatips/jw-javatip61.html&pageurl=http://www.javaworld.com/javaworld/javatips/jw-javatip61.html&site=jw_core

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;
    }
}

}

回答1:

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

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;

public class clipBoard{
//Creates the RTF string 
private static final String RTF_STRING = "{\\rtf1\\ansi\\deff0\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nThis line is the default color\\line\r\n\\cf2\r\nThis line is red\\line\r\n\\cf1\r\nThis line is the default color\r\n}\r\n}";
//Creates the plain text string
private static final String PLAIN_STRING = "This line is the default color \n This line is red \n This line is the default color";
//Array of data for specific flavor
private static final Object data[] = {new ByteArrayInputStream(RTF_STRING.getBytes()),new ByteArrayInputStream(PLAIN_STRING.getBytes())};
//Plain favor
private static final DataFlavor PLAIN_FLAVOR = new DataFlavor("text/plain", "Plain Flavor");
//RTF flavor
private static final DataFlavor RTF_FLAVOR = new DataFlavor("text/rtf", "Rich Formatted Text");
//Array of data flavors
private static final DataFlavor flavors[] = {RTF_FLAVOR,PLAIN_FLAVOR};

public static void main(String[] args){
    //Create clip board object
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    //Create transferable object
    Transferable p = new MyTransferable(data,flavors);
    //Transfer to clip board
    cb.setContents(p, null);
}

static class MyTransferable implements Transferable{
    //Array of data
    private Object dataA[] = null;
    //Array of flavors
    private DataFlavor flavorA[] = null;

    //Transferable class constructor
    public MyTransferable(Object data[], DataFlavor flavors[]){
        //Set the data passed in to the local variable
        dataA = data;
        //Set the flavors passes in to the local variable
        flavorA = flavors;
    }

    public Object getTransferData (DataFlavor df) throws UnsupportedFlavorException, IOException{
        //If text/rtf flavor is requested
        if (df.getMimeType().contains("text/rtf")){
            //Return text/rtf data
            return dataA[0];
        }
        //If plain flavor is requested
        else if (df.getMimeType().contains("text/plain")){
            //Return text/plain data
            return dataA[1];
        }
        else{
            throw new UnsupportedFlavorException(df);
        }
    }

    public boolean isDataFlavorSupported (DataFlavor df){
        //If the flavor is text/rtf or tet/plain return true
        if(df.getMimeType().contains("text/rtf") || df.getMimeType().contains("text/plain")){
            return true;
        }
        //Return false
        else{
            return false;
        }
    }

    public DataFlavor[] getTransferDataFlavors(){
        //Return array of flavors
        return flavorA;
    }
 }
}