RFR: 8159055: ImageIcon.setImage and ImageIcon(Image) constructor can't handle null parameter [v12]

Alexey Ivanov aivanov at openjdk.org
Thu Jul 3 09:46:45 UTC 2025


On Thu, 3 Jul 2025 06:46:25 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

> > Both image and description are two independent fields of ImageIcon object, each has its own getter and setter, and each can be changed independently. We should not enforce the order of calls: if an app developer wants to set the image to null, temporarily or not, the value of the description has to be preserved
> 
> I guess resetting the description came from the fact that description is mentioned as "Sets the description of the image. This is meant to be a brief textual description of the object." so if image is null, the description of the image logically should be nothing.

I agree that a description for a `null` image doesn't make sense.

Then, it also depends on how it's really handled. If the image in the `ImageIcon` object is `null`, the description could be ignored.

> But again, as pointed out, description has its own setter and getter so user would expect whatever description is stored either by [ImageIcon](https://docs.oracle.com/en/java/javase/24/docs/api/java.desktop/javax/swing/ImageIcon.html#%3Cinit%3E(java.awt.Image,java.lang.String))([Image](https://docs.oracle.com/en/java/javase/24/docs/api/java.desktop/java/awt/Image.html) image, [String](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/String.html) description) constructor or `setDescription `would be returned same via `getDescription`.. If not, the spec for set and getDescription needs to be modified to to incorporate null-image scenario.

Exactly.

Updating the specification for `setDescription` and `getDescription` to indicate the description will be set to `null` if the image is set to `null` will make the API cumbersome and less clear.

I prefer leaving the description intact.

> But all-in-all I guess this description change can go in separate PR along with [above change](https://github.com/openjdk/jdk/pull/25767#discussion_r2164102520)

Have anyone submit a bug to address this deficiency? If not, I'll submit one.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/25767#issuecomment-3031604978


More information about the client-libs-dev mailing list