JEditorPane : Java Glossary

JEditorPane

JEditorPane is a component in Swing that can be used to render HTML (Hypertext Markup Language) or RTF (Rich Text Format). JEditorPane can use HTMLEditorKit as its plug-in EditorKit. HTMLEditorKit is not a GUI (Graphic User Interface) component in its own right. JEditorPane allows display of formatted HTML or RTF with colours and fonts whereas JTextPane is like JEditorPane but with the additional ability to edit styles and character attributes.

Without any coding, with AWT, the user can select, copy and paste, even in an unsigned Applet. However, Applets must be signed for copy/paste to work in Swing. Copy/paste works in Swing without signing for applications.

JEditorPane needs perfect, validated, simple HTML. Don’t try to feed it wild HTML you screenscrape from websites on the net, e.g. HTML with unbalanced tags, broken links…. The JVM (Java Virtual Machine) will just hang. Only use it for rendering HTML you have generated yourself, ideally programmatically so you can be sure it contains no errors.

JEditorPane is simpler than the documentation suggests. You don’t need to get involved with Document structure. You can simply feed it raw HTML text. If you don’t embed the JEditorPane in a JScrollPane, the screen will twitch, a bug in Swing. You would think JEditorPane would be a subclass of JTextPane, but it is the reverse. In the constructor, or it setContentType pass it text/plain.

HTML

If you want to embed HTML formatting, (e.g. <u>…</u> to underline) do a setContentType( "text/html" ) and a setText with your text sandwiched in <html><body>…</body></html>.

JEditorPane.getText() will give you the HTML text indented and tidied with the DOCTYPE stripped off.

Gotchas

RobustJEditorPane

Any errors in the HTML rendered will trigger exceptions. RobustJEditorPane will trap the 

Learning More

Oracle’s Javadoc on JEditorPane class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/jeditorpane.html J:\mindprod\jgloss\jeditorpane.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.214]
You are visitor number 33,251.