String into HTMLDocument

I

Ike

If I have a string, in html format....

String s ="<html><b><h2>testing</h2></b><font color=red><br>is this in
red?</font>"

and I wish to display it, formatted, in a JEditorPane.....

JEditorPane messageTextArea = new JEditorPane();
HTMLDocument hdoc = new HTMLDocument();
messageTextArea.setDocument(hdoc);

How can I now set this string into the JEditorPane, and have it appear
formatted? I know I can do this with hdoc.setPage(URL) if I have a URL, but
I have a string, not a URL. I have tried

try{
insertString(0, s, attr);
}catch(javax.swing.text.BadLocationException ble){}

With no luck, as that only shows me the unformatted text. How can I set a
string of HTML text into an HTMLDocument ? Thanks Ike
 
R

Rhino

Ike said:
If I have a string, in html format....

String s ="<html><b><h2>testing</h2></b><font color=red><br>is this in
red?</font>"

and I wish to display it, formatted, in a JEditorPane.....

JEditorPane messageTextArea = new JEditorPane();
HTMLDocument hdoc = new HTMLDocument();
messageTextArea.setDocument(hdoc);

How can I now set this string into the JEditorPane, and have it appear
formatted? I know I can do this with hdoc.setPage(URL) if I have a URL, but
I have a string, not a URL. I have tried

try{
insertString(0, s, attr);
}catch(javax.swing.text.BadLocationException ble){}

With no luck, as that only shows me the unformatted text. How can I set a
string of HTML text into an HTMLDocument ? Thanks Ike
You'll need to do something like this:

JEditorPane formattedHtml = new JEditorPane();

formattedHtml.setContentType("text/html");

HTMLEditorKit2 myEditorKit = new HTMLEditorKit2();

StyleSheet myStyleSheet = getStyleSheet(myEditorKit);

HTMLDocument tipDocument = (HTMLDocument)
(myEditorKit.createDefaultDocument());

formattedHtml.setDocument(tipDocument);

formattedHtml.setText(s); //This is where you specify the String that is
supposed to appear in your document.


where this is the getStyleSheet() method:
private StyleSheet getStyleSheet(HTMLEditorKit2 editorKit) {

StyleSheet myStyleSheet = new StyleSheet();

myStyleSheet.addStyleSheet(editorKit.getStyleSheet());

editorKit.setStyleSheet(myStyleSheet);


/* Set the style sheet rules here by reading them from the constants
interface. */

for (int ix=0; ix<CSS_RULES.length; ix++) {

myStyleSheet.addRule(CSS_RULES[ix]);

}

return myStyleSheet;


}

and this is the HTMLEditorKit2 class:

package sdac;

import javax.swing.text.html.HTMLEditorKit;

import javax.swing.text.html.StyleSheet;

public class HTMLEditorKit2 extends HTMLEditorKit {


private StyleSheet styleSheet;

{

styleSheet = super.getStyleSheet();

}

public void setStyleSheet(StyleSheet value) {


styleSheet = value;

}

public StyleSheet getStyleSheet() {


return styleSheet;

}


}



and CSS_RULES looks something like this (you could, of course, have very
different CSS):

static final String[] CSS_RULES = {

"BODY {" +

" color: #666666;" +

" background-color: #FFDDDD;" +

" font-family: Arial, sans-serif;" +

" font-size: small;" +

" margin: 5px 10px 10px 5px;" +

" }"

};



Best regards,

Rhino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top