Issue
I am trying to change the icon of the Java application but nothing seems to work.
I am trying to get image from resources path using:
getClass().getResource("/AppIcon.png")
Sometimes I get an error like URL not found.
Solution
Solution; Everything works for windows but I am using Mac :D So I started to look around Taskbar class comes with awt package and found the solution (Thanks to flohall)
try {
var image = new ImageIcon(Objects.requireNonNull(Main.class.getResource("/AppIcon.png")));
frame.setIconImage(image.getImage());
if (System.getProperty("os.name").startsWith("Mac") || System.getProperty("os.name").startsWith("Darwin")) {
Taskbar taskbar = Taskbar.getTaskbar();
try {
taskbar.setIconImage(image.getImage());
} catch (final UnsupportedOperationException e) {
System.out.println("Can't set taskbar icon.");
} catch (final SecurityException e) {
System.out.println("Warning. Can't set taskbar icon due to security exceptions.");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
So we are telling the taskbar to change its icon using built-in awt Taskbar class on taskbar.setIconImage(image.getImage());
. And that solves most of the things I've needed for.
Answered By - devsimsek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.