The following code works fine when running on NetBeans.
this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage("PlagiaLyzerIcon.png"));
However, once it was built into Jar file, the icon was gone.
Anyone has idea what's the problem? I realized I've to put the icon image on the root directory, however, after compiling into JAR, the icon gone.
Thanks for any help ...
Hi everyone, the problem was solved with the following code,
this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("plagialyzer/resources/PlagiaLyzerIcon.png")));
It works once compiled into jar file.
Use
this.getFrame().setIconImage(
new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png"))
);
instead.
Note:
this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:
getResource("yourfolder/PlagiaLyzerIcon.png")
That is because the Netbeans IDE has a different classpath, than when running the jar-file stand-alone (without Ant).
Assume your Netbeans project is at location /project/
:
The classpath is: /project/build/classes/
and the project root /project/
. If your icons are stored in: /project/myicons/
, then they are part of the classpath, since /project/
is too. But when you build your project, only files in /project/build/classes/
will eventually end up in the jar-file, these files are "build" from /projcet/src/
.
Solution:
Move your icons into a source-package: /project/src/myicons/
Or, add the /project/myicons/
folder to your sources (right-click your project -> Properties -> Sources -> add your folder there)
Thanks a lot...
Here the code that works for me!!!!
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("images/frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
super.setIconImage(image);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With