I'm trying to change the icon image of my jframe using an IconImage object but I realised that I need to use an Image object to do so, however an Image object is an abstract class and the subclasses for them that I have found are very confusing to use, could someone run me through how to change my image icon in JFrame?
Asked
Active
Viewed 82 times
1 Answers
0
use this code to change icon:
myFrame = new JFrame("Test");
myFrame.setIconImage(new ImageIcon(url).getImage());

Marwen Jaffel
- 703
- 1
- 6
- 14
-
`new ImageIcon(url).getImage()` this is fragile code. Better to replace it with reading the image directly using `ImageIO`. The `ImageIO` methods will throw helpful exceptions if the image is not found. Actually thinking about it, the `url` implies an `URL`. Calling `getResource` with an invalid path will result in a `null` URL, and (AFAIR) trying to establish an `ImageIcon` using a `null` parameter will cause a `NullPointerException`. My comment was directed towards those who use the `String` constructor for an image icon. That will fail (regularly and) silently. – Andrew Thompson Jan 20 '20 at 12:05