开发者

Output a JTextPane's content to an HTML file

I am working on a bug in this program where it should be able to save the contents of a JTextPane to a formatted HTML file. The code that is currently there save a file containing just the tags but no content. The page appears bla开发者_如何学Cnk but all the correct <p> and <font> tags are there with the correct colour set etc.

HTMLDocument htmlDoc = (HTMLDocument) output.getDocument();
FileWriter fw = new FileWriter(logFile);
HTMLWriter htmlWriter = new HTMLWriter(fw, htmlDoc);
htmlWriter.write();
fw.flush();
fw.close();

That is the code as it is currently. output is the JTextPane object. It doesn't seem right to be that the Document object returned from getDocument() is being casted directly into a HTMLDocument object.

Does anyone know what would make this work?


The JTextPane constructor takes a StyledDocument as argument.
I suspect that output must have been created by passing an HTMLDocument (which implements StyledDocument) to its constructor.

So as long as you know that output was created with an HTMLDocument, or that you have called output.setDocument(new HTMLDocument()) or something, it is safe to do that cast!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜