Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to output JTextPane styled contents to HTML, including custom style?
    primarykey
    data
    text
    <p>I currently use a JTextPane to allow users to add/edit text. It allows bold/italic/underline (and I plan to allow links in the future). It also allows users to drop buttons in, which are inserted as custom styles. The panel looks like:</p> <p><strong><em>&lt; &lt; image deleted > ></em></strong></p> <p>I would like to be able to save/load content as HTML - the content will be incorporated in a Flash swf. I am able to get content as HTML like so:</p> <pre><code> public String getHTMLText(){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); try{ HTMLEditorKit hk = new HTMLEditorKit(); hk.write(baos, this.getStyledDocument(), 0, this.getDocument().getLength()); } catch (IOException e) { e.printStackTrace(); } catch (BadLocationException e) { e.printStackTrace(); } return baos.toString(); } </code></pre> <p>This works fine if the JTextPane includes only bold/italic/underlined text. The output is overly complicated though. I want to be able to output my custom style as well, but when I try I'm getting this error:</p> <pre><code> Exception occurred during event dispatching: java.lang.NullPointerException at javax.swing.text.html.MinimalHTMLWriter.writeAttributes(MinimalHTMLWriter.java:151) at javax.swing.text.html.MinimalHTMLWriter.writeStyles(MinimalHTMLWriter.java:256) at javax.swing.text.html.MinimalHTMLWriter.writeHeader(MinimalHTMLWriter.java:220) at javax.swing.text.html.MinimalHTMLWriter.write(MinimalHTMLWriter.java:122) at javax.swing.text.html.HTMLEditorKit.write(HTMLEditorKit.java:293) at javax.swing.text.DefaultEditorKit.write(DefaultEditorKit.java:152) at numeracy.referencetextpanel.NRefButtonTextArea.getHTMLText(NRefButtonTextArea.java:328) at numeracy.referencetextpanel.NInputPanelRefTextButton.getReferencedText(NInputPanelRefTextButton.java:59) at numeracy.referencetextpanel.NInputRefText.actionPerformed(NInputRefText.java:106) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) </code></pre> <p>My custom style is inserted like so (cID is a string like "{0-0}"):</p> <pre><code>StyledDocument doc = this.getStyledDocument(); NRefButton b = this.createRefButton(cID); Style style = doc.addStyle(cID, null); //prepare a style StyleConstants.setComponent(style, b); doc.insertString(doc.getLength(), b.toString(), style); //insert button at index </code></pre> <p>The function createRefButton(String cID):</p> <pre><code> private NRefButton createRefButton(String cID) { NRefButton b = new NRefButton(_equationButtons.get(cID).getText(), cID, _equationButtons.get(cID).isStruck()); //prepare a button return b; } </code></pre> <p>NRefButton overrides toString, which returns "{"+cID+"}".</p> <p>What I would like to know: should I modify the way I insert the "Style" to get this error? </p> <p>Is there a different/better way for me to get HTML from this JTextPane? All I want is HTML tags around bold/italic/underlined text, not overly complicated as if it is I'll then have to strip out the unnecessary HTML, and for the "style" to come out as button.toString(). </p> <p>Or should I implement my own toHTML() method, wrapping bold/italic/underlined text with the required tags? I don't mind doing this (in some ways I'd prefer it), but I don't know how to get the styles for the given JTextPane document. I suppose if I were able to get these styles, I could iterate through them, wrapping styled text in the appropriate tags?</p> <p>Ideally, the pictured JTextPane content would be output as:</p> <pre><code>&lt;html&gt;&lt;p&gt;This is some &lt;b&gt;styled&lt;/b&gt; text. It is &lt;u&gt;incredible&lt;/u&gt;. &lt;br/&gt; &lt;br/&gt; Here we have a button that has been dropped in: {0-0}. These buttons are a &lt;b&gt;&lt;i&gt;required part of this project.&lt;/i&gt;&lt;/b&gt; </code></pre> <p>I want to be able to <em>read</em> the output HTML <em>into</em> the JTextPane as well - again I don't mind writing my own fromHTML() method for this, but I need to be able to get HTML out first.</p> <p>Thank you for taking the time to read my question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload