0

I looked at other answers and tried:

ImageIcon img = new ImageIcon(url);
setIconImage(img.getImage());

and:

URL imgURL = getClass().getClassLoader().getResource(url);
if (imgURL != null) {
    System.out.println("Found icon image: "+imgURL);
    Image image=Toolkit.getDefaultToolkit().getImage(imgURL);
    setIconImage(image);
} else {
    System.err.println("Could not find icon image");
}

Within the JFrame class and I put the image file in the resource folder, and also the same folder as my .java files and the root folder of my project and even included the "/" symbol at the beginning of the URL string but nothing is working. I was wondering if anyone tried it lately and got it working?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    what's inside `url` string? – Crih.exe Aug 07 '21 at 22:54
  • Create a resources folder for your project. Put the image file inside the resources folder. Put the resources folder on the classpath. Read the answers to this [Stack Overflow question](https://stackoverflow.com/questions/27934796/how-do-i-add-a-resources-folder-to-my-java-project-in-eclipse). – Gilbert Le Blanc Aug 08 '21 at 00:11
  • 1) Change `getClass().getClassLoader().getResource(url);` to simply `getClass().getResource(url);`. The class loader is not intended for application resources. 2) Use `ImageIO` to load the image. It will give helpful output in the event of a problem (via `Throwable.printStackTrace()`. 3) Put the image inside a `resources` path at the root of the class path (not within another package) and start the `String` with `/resources/..` – Andrew Thompson Aug 08 '21 at 01:03
  • I added a resources folder as per the first answer to the link provided by Gilbert and I tried the suggestions provided. it is able to find the file but for some reason it does not change the icon image. – Valencia Starr Aug 09 '21 at 23:16
  • It looks like the resources folder is added to the path and the url is "icon.png" and I also tried "/resources/" but it cannot find the file if I do that. But it finds it with "icon.png". – Valencia Starr Aug 09 '21 at 23:18
  • Update: I tried it on a windows device and it worked on that. It is the mac it does not work. – Valencia Starr Aug 10 '21 at 00:39
  • *"the link provided by Gilbert"* Tip: Add @GilbertLeBlanc (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Aug 10 '21 at 01:23

2 Answers2

0

I don't know what's inside your url string but there is another way to set the icon to a JFrame:

frame.setIconImage((Image)ImageIO.read(getClass().getResourceAsStream("/imagename.png")));

The / symbol means that your path is relative to your project root.

EDIT:

JFrame.setIconImage(Image image) doesn't work on macOS.

Instead you can use Application.getApplication().setDockIconImage(Image image) of the com.apple.eawt.Application package to get it work. (as mentioned by @Valencia Starr)

Crih.exe
  • 502
  • 4
  • 16
  • The url is "icon.png". It is able to find the file and the resources folder appear to be added to the path but for some reason the icon image does not change. – Valencia Starr Aug 09 '21 at 23:19
  • 1
    Update: I tried it on a windows device and it worked on that. It is the mac it does not work. – Valencia Starr Aug 10 '21 at 00:39
  • @ValenciaStarr It seems that macOS doesn't support jframe icons. I can't try it right now, but you should be able to change the icon running your jar using this parameter: `Xdock:icon="/path/to/myicon.icns"`. Let me know if it works so I'll edit the answer. – Crih.exe Aug 10 '21 at 00:50
  • 1
    I got it to work. I imported the com.apple.eawt.Application package and used Application.getApplication().setDockIconImage(image) which works on mac not windows. So apparently Macs come installed with the library and I also had to make sure I had Java 1.8 in my build path. So there setIconImage(Image img) works for Windows and Application.getApplication().setDockIconImage(Image img) works for macs. Thank you very much for your reply and help. It is greatly appreciated. – Valencia Starr Aug 10 '21 at 02:26
0

I got it to work.

So apparently Macs come installed with a library and I also had to make sure I had Java 1.8 in my build path.

I imported the com.apple.eawt.Application package and used Application.getApplication().setDockIconImage(image) which works on macOS and not on Windows.

Crih.exe
  • 502
  • 4
  • 16