Does JEditorPane have problems with Charset when displaying HTML?

I have the following code:

import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; public class ScratchPad { public static void main(String args[]) throws Exception { String html ="<html>"+ "<head>"+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>"+ // this is the problem right here "<title>Error 400 BAD_REQUEST</title>"+ "</head>"+ "<body>"+ "<h2>HTTP ERROR: 400</h2><pre>BAD_REQUEST</pre>"+ "<p>RequestURI=null</p>"+ "<p><i><small><a href=\"http://jetty.mortbay.org\">Powered by jetty://</a></small></i></p>"+ "</body>"+ "</html>"; JFrame f = new JFrame(); JEditorPane editor = new JEditorPane(); editor.setEditable( false ); editor.getDocument().putProperty( "Ignore-Charset", "true" ); // this line makes no difference either way editor.setContentType( "text/html" ); editor.setText( html ); f.add( new JScrollPane(editor, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER) ); f.pack(); f.setVisible( true ); } } 

If you run it, you will notice that the frame is empty. However, if I remove the "; charset = ISO-8859-1" meta tag, HTML will appear. Any ideas why and what I can do to prevent this (other than manually hacking an HTML string that I have no control over ...).

Edit # 1 - putProperty ("Ignore-Charset", "true") has no meaning, unfortunately.

+4
source share
2 answers

Use the following line before setText and after setContentType.

 editor.getDocument().putProperty("IgnoreCharsetDirective", Boolean.TRUE); 

This is one of the mystical undocumented features. setContentType creates a new document that has no effect if you set it earlier.

+14
source

When I run the code, I can only see HTML text when I delete the meta strong> line. Perhaps this has something to do with the character settings in the system in which it works.

0
source

Source: https://habr.com/ru/post/1277527/


All Articles