简体   繁体   中英

Problem With HTML display in JEditorPane java

Im trying to display html in a JEditorPane. Initially the type is set to "text/html".

When I use setPage(URL) it works fine and the resulting output is displayed but If I have a String that contains HTML code and I used setText(String) to display the result on the JEditorPane nothing is displayed I see only white space.

Of-course if I copied the whats in the string pasted it in notpad, saved it as .html then opened the resulting file in the browser it displayed correctly. The real problem is in how or what the JEditorPane does with the string inorder to display whats inside it. The JEditorPane is inside a JscrollPane which is inside a Jframe. and I only used setContentType( "text/html" ) and setText(String) methods for html display.

Is there anyway to get around this than wrting the resulting html code to a file and using SetPage(URL)? I can post the html code if you need it (but its quite large). Thanks for your help.

Don't know why setText does not work. But here is a workaround.

Try this URL. (the whole file in the URL) (This is what Android's WebView calls when you setText in it)

data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2Ftitle%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A%0D%0A

It starts with data:text/html;charset=utf-8, and is followed by your HTML.

However you do have to encode it.. At least you have to replace % with %25 The rest might just work without encoding though.

You can also use this code to embed images without calling a file

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC" />

You just have to base64 encode your image and then you can paste it right in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM