How to display an image in JEditorPane with HTML
If you want to make an image appear in a
JEditorPane that contains HTML code, this solution works just fine:
import javax.swing.*;
import java.awt.*;
class Image extends JFrame {
public static void main(String[] args) throws Exception {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
String src = Image.class.getClassLoader().
getSystemResource("image.jpg").toString();
frame.getContentPane().add(new JEditorPane("text/html",
" < img src='"+src+"' width=280 height=400> "));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Output:
The .jpg image must be placed in the same directory in the EditorPaneImage.class file. When your code is compiled, java calls the method
getSystemResource(jpgname). The advantage is that your application works very well when you create and run from the .jar file.