I am using JeditorPane and JEditorKit to display some HTML. The HTML is displaying correctly but the images show up as broken (they display properly in a browser). The image src is base64. I set the content type thus:
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");
I'm guessing that because it contains both text and images, the content type is incorrect. If that is the case, what should it be set to? TIA.
** After Madushan Perera's reply**
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");
ed.setEditable(false);
HTMLDocument html=(HTMLDocument) ed.getDocument();
html.putProperty("IgnoreCharsetDirective", new Boolean(true));
HTMLEditorKit htmle=(HTMLEditorKit) ed.getEditorKit();
try {
htmle.insertHTML(html,html.getLength(),content,0,0,null);
} catch (BadLocationException | IOException e) {
// Should not get here
e.printStackTrace();
}
ed.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent pE) {
if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType()) {
String desc = pE.getDescription();
if (desc == null || !desc.startsWith("#")) return;
desc = desc.substring(1);
ed.scrollToReference(desc);
}
}
});
ed.setCaretPosition(0);
JScrollPane scroll=new JScrollPane(ed,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel jp=new JPanel();
Dimension size=new Dimension(700,700);
jp.setPreferredSize(size);
jp.setLayout(new BorderLayout());
jp.add(scroll);
JOptionPane.showMessageDialog(null,jp,title,JOptionPane.INFORMATION_MESSAGE);
'html' is the string containing the HTML. It was read from an html file type using IOUtils.toString. I'm probably going to have to develop an SCCE.
Implementing CustomEditor
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");
ed.setEditable(false);
CustomToolKit htmle=new CustomToolKit();
ed.setEditorKit(htmle);
String content=readFile(fileName_+".html").replaceAll("(\\r|\\n)", "");
content=content.replace("!!!!",VERSION.VERSION);
ed.setText(content);
ed.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent pE) {
if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType()) {
String desc = pE.getDescription();
if (desc == null || !desc.startsWith("#")) return;
desc = desc.substring(1);
ed.scrollToReference(desc);
}
}
});
ed.setCaretPosition(0);
JScrollPane scroll=new JScrollPane(ed,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel jp=new JPanel();
Dimension size=new Dimension(700,700);
jp.setPreferredSize(size);
jp.setLayout(new BorderLayout());
jp.add(scroll);
JOptionPane.showMessageDialog(null,jp,title,JOptionPane.INFORMATION_MESSAGE);
}
Now I get nothing. Obviously I did not properly implement your suggestion.
You can try something like below :
UPDATE :
UPDATE 2 :
You have to create custom toolkit for the
JEditorPane
like below :Then you have to override the
getImageURL()
ofjavax.swing.text.html.ImageView
to support Base64 encoded images:Finally you can set your
CustomToolKit
to youreditorPane
: