问题
I'm trying to create a WYSIWYG editor using JTextPane.
I'm using DefaultEditorKit.CopyAction to copy text in the editor. But this method does not preserve the style of the text. Can someone tell me how to copy the text in JTextPane and preserve the style, please ?
回答1:
http://java-sl.com/tip_merge_documents.html You can use this. If you need part of the document just select desired fragment of the source pane.
回答2:
I have a class that uses the following code to copy all the text out of the StyledDocument into the user's clipboard; it appears to preserve attributes like color, bold, and underline (haven't tested it with anything else). Note that "this.doc" is a StyledDocument.
No guarantees that this is the best method.
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();
}
If you want to only copy a subsection of the document, I think you want to modify this line:
rtfek.write( baos, this.doc, int startPosition, int endPosition )
Edit: It turns out that whoever created RTFEditorKit decided that they did not need to adhere to their API. Essentially the startPosition and endPosition above have no effect.
/**
* 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);
}
回答3:
The book publisher Manning offers a free download of the first edition of "Swing" by Matthew Robinson and Pavel Vorobiev at http://www.manning.com/robinson2. (Scroll down the page looking for the link "Download Complete Swing Book (MS Word 97).")
Chapter 20 talks about developing a WYSIWYG RTF editor using a JTextPane
as part of the editing component. The new version of the book is revised and describes the creation of a WYSIWYG HTML editor, but it is not free. (Despite what the page at the link says, the paper copy of the new edition does not appear to be available, but the eBook is, if you're interested.)
This was a great resource for me when I was trying to do something similar.
回答4:
Try to use serialisation. Something like
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;
}
Spied on Cay Horstmann`s book http://horstmann.com/corejava.html
来源:https://stackoverflow.com/questions/1696046/how-to-copy-styled-text-in-jtextpane