JDK-8224475 : JTextPane does not show images in HTML rendering
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 8u202,11.0.2,12.0.1,13
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86_64
  • Submitted: 2019-05-16
  • Updated: 2020-06-04
  • Resolved: 2020-01-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 15 JDK 8 Other
11.0.7-oracleFixed 13.0.4Fixed 15 b07Fixed 8u251Fixed openjdk8u262Fixed
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
System/OS : 
Debian 9, 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64 GNU/Linux
Java :
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1~deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.212-b01, mixed mode)

A DESCRIPTION OF THE PROBLEM :
In a JTextPane I put HTML with some images. Those images were previously put in the JTextPane images cache using "jTextPane.getDocument().putProperty("imageCache",cache)". It has been working fine for years, and works with Oracle JDK. But since OpenJDK 8 212 (may be on earlier releases too) it seems images get lost. It works randomly at the very beginning displaying some of the images, then if I refresh the component images are nor displayed anymore.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the below provided code with java version :
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1~deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.212-b01, mixed mode)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Green arrow is displayed on all iterations.
ACTUAL -
With java version OpenJDK the green arrow is not displayed after a few iteration.
With java version Oracle Java(TM) SE Runtime Environment (build 1.8.0_172-b11) the green arrow in displayed on all iterations.

---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import java.awt.Image;
import java.net.URL;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.EditorKit;

public class MainBug implements Runnable {
	private static JFrame frame;
	public static JPanel contentPanel;
	static Dictionary<URL, Image> cache;
	static JTextPane jTextPane;
    static URL urlArrow;

	public static void main(String[] args) {
		try
		{
			urlArrow = new URL("http:\\arrow.png");
			
			frame = new JFrame();
			frame.setBounds(200, 200, 800, 600);
			frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			frame.getContentPane().setLayout(new BorderLayout(0, 0));
			frame.setTitle("JTextPane HTML Images bug demo");
			jTextPane = new JTextPane();
			frame.getContentPane().add(contentPanel = new JPanel());
			contentPanel.setLayout(new BorderLayout(0, 0));
			contentPanel.add(jTextPane);
			final EditorKit l_kit = jTextPane.getEditorKitForContentType("text/html");
			jTextPane.setEditable(false);
			jTextPane.setEditorKit(l_kit);
			frame.getContentPane().add(jTextPane, BorderLayout.CENTER);
			cache = (Dictionary<URL, Image>)jTextPane.getDocument().getProperty("imageCache");
            if (cache==null) {
				cache=new Hashtable<URL, Image>();
				jTextPane.getDocument().putProperty("imageCache",cache);
			}
	        Image l_greenequalImage = new ImageIcon(MainBug.class.getResource("arrow.png")).getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH);
	        cache.put(urlArrow, l_greenequalImage);
	        frame.setVisible(true);
	        new Thread(new MainBug()).start();
		}
		catch(Exception e)
		{
			System.err.println(e);
			e.printStackTrace();
		}

	}
	public void run() {
		for (int i=0; i < 5; i++)
		{
			jTextPane.setText("<html><body bgcolor=\"#666666\"><center>Iteration " + i + " -> <img src=\"" + urlArrow + "\"> </center></body></html>");
			jTextPane.validate();
			jTextPane.repaint();
			try {
				Thread.currentThread().sleep(2000);
			} catch (InterruptedException e) { System.err.println(e); }
		}
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use java version:
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)


FREQUENCY : always



Comments
Fix request (13u) Requesting backport to 13u for parity with 11u, applies cleanly.
04-06-2020

This was pushed into Oracle's 8u in early January, so there's been about one and a half months when it could have been backported as part of the development cycle. 8u252-b06 will be merged into 8u once tagged and tested.
11-03-2020

Reluctantly targeting 8u262. I won't be able to push this, though, until JDK-8230235 (a 8u252 fix) is merged back into jdk8u-dev.
10-03-2020

Right, but I don't see any case for making this a critical fix for 8u252. Please target 8u262.
10-03-2020

Fix Request (OpenJDK 8u): Please approve backporting this fix to OpenJDK 8u252. It's a patch also in Oracle JDK 8u251 and it applies clean - the JDK 15 patch[*] - post unshuffeling and after JDK-8230235 has been applied (also a clean backport). Regression test fails prior patch, passes after. [*] The JDK 11u patch had a bug which didn't include arrow.png. See JDK-8240724.
09-03-2020

jdk11 downport request I would like to have the patch in openjdk11 as well, because it is a part of 11.0.7-oracle. The patch applies cleanly to 11u.
28-02-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/66064712e67d User: psadhukhan Date: 2020-01-22 08:47:51 +0000
22-01-2020

URL: https://hg.openjdk.java.net/jdk/client/rev/66064712e67d User: pbansal Date: 2020-01-09 09:43:49 +0000
09-01-2020

[~pbansal] it seems this issue is also a regression of JDK-8208638 which is a regression of JDK-8218674, whose another regression you are working on JDK-8230235. Probably the root cause is same.
19-11-2019

As per description, images added with HTML using "jTextPane.getDocument().putProperty("imageCache",cache)" fails to render correctly. Issue is reported JDK 8u212, uppon verification confirmed as regression in 8u202 and 11.0.2. Result: ======= 8: OK 8u201: OK 8u202: Fail 8u212: Fail 11.0.1: OK 11.0.2: Fail 13 ea b20: Fail This is a Regression in JDK 8u202 and 11.0.2. To verify, run the attached test case with respective JDK version.
21-05-2019