Formatted cliboard Java

余生长醉 提交于 2019-12-01 08:52:01

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;
    }
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!