I have searched and haven't seen anyone experiencing what I am.. yet, but this is the issue I am having:
I am trying to set a small bit of HTML as the text of my JEditorPane. Here is the code:
JEditorPane htmlPane = new JEditorPane();
String imageString = "<img src=\"http://tfwiki.net/mediawiki/images2/thumb/3/37/Optimusg1.jpg/350px-Optimusg1.jpg\"/>";
String description = "<table width=300 border=0 cellspacing=0></table>" + imageString + "</table>";
htmlPane.setContentType("text/html");
htmlPane.setText(description);
but after I call setText, my editor pane contents are:
<html>
<head>
</head>
<body>
</body>
</html>
I have tried variations of adding <html>
and </html>
to the beginning and end of my string, but no luck. Anyone know what I am missing or doing wrong?
I am using Java 1.7.0_55 32-bit.
After a little testing I found that...
JEditorPane
will accept it, and in fact, it seems to do some of it's own validation, removing invalid tags...fun stuff<tr><td>...</td></tr>
into the tableJEditorPane
, even though the same HTML would load the image in a browser (like Chrome)1
and add an alt
tag to the image, which helped verify certain elements where actually been rendered...import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestEditorPane {
public static void main(String[] args) {
new TestEditorPane();
}
public TestEditorPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JEditorPane htmlPane = new JEditorPane();
String description = "<html><body>Hello<table border=1><tr><td><img alt='Bad' src='http://fc07.deviantart.net/fs70/i/2012/084/c/0/angry_wet_ponies_are_angry____by_tabby444-d4tyfsc.png'/></tr></td></table></body></html>";
htmlPane.setContentType("text/html");
htmlPane.setText(description);
System.out.println(htmlPane.getText());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(htmlPane));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}