I want to insert an image (say – a formula compiled from LaTeX) into JTextPane, using HTML+CSS for text markup, manually setting it’s size. I tried like here
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();
}
}
For some reason JTextPane works perfectly with text styles, but ignores CSS notations for pictures. Is there any way to handle this out or I’m just wasting time?
5
As Gilbert Le Blank
mentioned in the comment, the width
block shall be in <img>
statement. So the HTML string should look like this:
<html>
<body>
<p color="red"> Aaaa. </p>
<img width="250" src="/path/to/Image" />
</body>
</html>