I am looking to add an image (such as a formula created from LaTeX) into JTextPane, using HTML+CSS for text formatting and manually setting its size. I attempted the following approach:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JTextPane;
public class App extends JFrame {
private void run() {
setSize(new Dimension(320, 240));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
String imageUrl = getClass()
.getClassLoader()
.getResource("resources/img.jpg");
pane.setText(
"<html>"+
"<head> <style>"+
// CSS works perfectly for text,
".text { color: red; font-family: \"Times New Roman\"; }"+
// but not for images
".pic { width: 250px; }"+
"</style> </head>"+
"<body>"+
"<p class=\"text\"> Aaaa. </p>"+
"<img class=\"pic\" src=\""+imageUrl+"\" />"+
"</body>"+
"</html>"
);
getContentPane().add(pane);
setVisible(true);
};
public App() {
super("Test HTML+CSS");
}
public static void main(String[] args) {
App app = new App();
app.run();
}
}
It appears that the JTextPane effectively applies text styles with CSS, but does not seem to respond to CSS rules for images. Is there a way to resolve this issue or am I simply wasting time?