Formatted cliboard Java

后端 未结 1 548
南方客
南方客 2021-01-06 06:56

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

1条回答
  •  迷失自我
    2021-01-06 07:34

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

    0 讨论(0)
提交回复
热议问题