Java formatted clickboard

I am having a problem trying to figure out how to preserve text formatting in a Java program while saving to the system clipboard.

It doesn't work with things like Microsoft Wordpad or Lotus Symphony. On the contrary, if I create a formatted string in Word and copy it, then it works correctly with all the trial cases where I try to paste it.

I don't want to use external sources like org.eclipse. *.

Here are some links that I have compiled that might help me to point in the right direction.

It seems to me that I am not using the appropriate 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 doesn't shed much light on which formatted text can be used. I do understand that this may not be the same for every OS and I need to check to see if it is supported on the OS being used.

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 your help on advanced, I really appreciate it!

Dan

EDIT

I am currently using the code from http://lists.apple.com/archives/java-dev/2004/Jul/msg00359.html with slight modifications. The problem I am currently having is that I need to transfer data to the clipboard in two different formats. "text / rtf" and "text / plain", seeing that some programs do not support RTF. I am using the inside clipboard to see what is inside the clipboard. I can successfully post RTF or plain text, but not both at the same time. When I do this, only the last one is added. Your help is greatly appreciated!

In general, I cannot set the clipboard with two different data attributes 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;
    }
}

      

}

+3


source to share


1 answer


After a lot of searching and trial and error and help from friend Sebastian and Logan, it seems to have cleared up. This allows multiple data formats to be stored on the clip board at the same time while maintaining the style and formatting of the text. Hope this helps someone. It 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;
    }
 }
}

      

+5


source







All Articles