简体   繁体   中英

Insert string in clipboard without lost formatted text / style

I need to insert a string at the beginning of the clipboard without losing inline formatting of text containing the same. Currently this is not happening in my code!

public String getClipboard()
{           
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    try
    {   
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor))
        {
            String text = (String) t.getTransferData(DataFlavor.stringFlavor);
            return text;
        }
    }
    catch (UnsupportedFlavorException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return null;
}

public void setClipboard(String str)
{
    StringSelection ss = new StringSelection(str);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, this);  
}

public void uso()
{
    String claveVM = "Text I want insert";  
    setClipboard(claveVM + getClipboard());
}

You can't do it with plain text (string). You need RTF or HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM