如何在JTextPane中复制样式文本

时间:2009-11-08 11:06:45

标签: java swing copy copy-paste jtextpane

我正在尝试使用JTextPane创建一个WYSIWYG编辑器。

我正在使用DefaultEditorKit.CopyAction在编辑器中复制文本。但是这种方法不保留文本的样式。有人能告诉我如何在JTextPane中复制文本并保留样式吗?

4 个答案:

答案 0 :(得分:3)

http://java-sl.com/tip_merge_documents.html 你可以用它。如果您需要文档的一部分,只需选择源窗格的所需片段。

答案 1 :(得分:1)

我有一个类使用以下代码将StyledDocument中的所有文本复制到用户的剪贴板中;它似乎保留了颜色,粗体和下划线等属性(没有用其他任何东西测试它)。请注意,“this.doc”是StyledDocument。

不保证这是最好的方法。

    try 
    {
        Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
        RTFEditorKit rtfek = new RTFEditorKit();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        rtfek.write( baos, this.doc, 0, this.doc.getLength() );
        baos.flush();
        DataHandler dh = new DataHandler( baos.toByteArray(), rtfek.getContentType() );
        clpbrd.setContents(dh, null);
    }
    catch ( IOException | BadLocationException e ) 
    {
        e.printStackTrace();
    }

如果您只想复制文档的一个子部分,我想您想要修改这一行:

rtfek.write( baos, this.doc, int startPosition, int endPosition )

编辑:事实证明,创建RTFEditorKit的人决定他们不需要遵守他们的API。基本上上面的startPosition和endPosition都没有效果。

/**
 * Write content from a document to the given stream
 * in a format appropriate for this kind of content handler.
 *
 * @param out  The stream to write to
 * @param doc The source for the write.
 * @param pos The location in the document to fetch the
 *   content.
 * @param len The amount to write out.
 * @exception IOException on any I/O error
 * @exception BadLocationException if pos represents an invalid
 *   location within the document.
 */
public void write(OutputStream out, Document doc, int pos, int len)
    throws IOException, BadLocationException {

        // PENDING(prinz) this needs to be fixed to
        // use the given document range.
        RTFGenerator.writeDocument(doc, out);
}

答案 2 :(得分:0)

图书出版商Manning在http://www.manning.com/robinson2免费下载Matthew Robinson和Pavel Vorobiev的第一版“Swing”。 (向下滚动页面,查找链接“下载完整的Swing Book(MS Word 97)。”)

第20章讨论了使用JTextPane作为编辑组件的一部分来开发WYSIWYG RTF编辑器。本书的新版本已经过修订,并描述了WYSIWYG HTML编辑器的创建,但它并不是免费的。 (尽管链接上的页面显示,新版本的纸质副本似乎不可用,但如果您感兴趣,则电子书是。)

当我尝试做类似的事情时,这对我来说是一个很好的资源。

答案 3 :(得分:0)

尝试使用序列化。 像

这样的东西
public static DefaultStyledDocument cloneStyledDoc(DefaultStyledDocument source) {
    try {
        DefaultStyledDocument retDoc = new DefaultStyledDocument();     

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(source); // write object to byte stream

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray() );               
        ObjectInputStream ois = new ObjectInputStream(bis);
        retDoc = (DefaultStyledDocument) ois.readObject(); //read object from stream
        ois.close();        
      return retDoc;
    } 
    catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;        
}

发现了Cay Horstmann的书http://horstmann.com/corejava.html

相关问题