How to change default app icon in Java?

I am using NetBeans trying to change the familiar Java cup of coffee icon to a png file, which I saved in the resource directory in the jar file. I found many different web pages that claim to have a solution, but none of them work yet.

Here is what I have at the moment (excluding the try-catch block):

URL url = new URL("com/xyz/resources/camera.png"); Toolkit kit = Toolkit.getDefaultToolkit(); Image img = kit.createImage(url); getFrame().setIconImage(img); 

The class containing this code is in the com.xyz package, if that matters. This class also extends JFrame. This code throws a MalformedUrlException on the first line.

Does anyone have a solution that works?

+60
java icons
Oct. 16 '08 at 18:58
source share
10 answers
 java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png"); 

May or may not need a '/' at the beginning of the path.

+69
Oct. 16 '08 at 19:01
source share

You can simply run Netbeans in the project view, go to the JFrame property, select the icon image property, select the Set Form iconImage property using: "Custom Code", and then enter the following code in the Form.SetIconImage() function:

 Toolkit.getDefaultToolkit().getImage(name_of_your_JFrame.class.getResource("image.png")) 

Do not forget to import:

 import java.awt.Toolkit; 

in the source code!

+13
Jan 16 '13 at 9:41
source share

Or put the image in a location relative to the class, and you do not need all the information about this package / path in the line itself.

 com.xyz.SomeClassInThisPackage.class.getResource( "resources/camera.png" ); 

Thus, if you move the class to another package, you will not need to find all the lines, just move the class and its resource directory.

+4
Oct. 16 '08 at 20:37
source share

Try this post after

 initcomponents(); setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address"))); 
+4
Oct 19 '13 at 18:46
source share
  /** Creates new form Java Program1*/ public Java Program1() Image im = null; try { im = ImageIO.read(getClass().getResource("/image location")); } catch (IOException ex) { Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex); } setIconImage(im); 

This is what I used in the netbeans GUI and it worked great

+3
Oct 24
source share

In a class that extends the use of javax.swing.JFrame setIconImage .

 this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage()); 
+1
Sep 21 '13 at 0:58
source share

You have to define icons of different sizes, Windows and Linux distributions, for example, Ubuntu use different icons on the taskbar and Alt-Tab.

 public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png"); public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png"); public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png"); List<Image> images = new ArrayList<>(); try { images.add(ImageIO.read(HelperUi.ICON96)); images.add(ImageIO.read(HelperUi.ICON32)); images.add(ImageIO.read(HelperUi.ICON16)); } catch (IOException e) { LOGGER.error(e, e); } // Define a small and large app icon this.setIconImages(images); 
+1
Jul 24 '16 at 0:48
source share

Example:

 URL imageURL = this.getClass().getClassLoader().getResource("Gui/icon/report-go-icon.png"); ImageIcon iChing = new ImageIcon("C:\\Users\\RrezartP\\Documents\\NetBeansProjects\\Inventari\\src\\Gui\\icon\\report-go-icon.png"); btnReport.setIcon(iChing); System.out.println(imageURL); 
0
Sep 26 '14 at 9:01
source share

inside the frame constructor

 try{ setIconImage(ImageIO.read(new File("./images/icon.png"))); } catch (Exception ex){ //do something } 
0
Oct 21 '17 at 18:32
source share

You can try this one , it works just fine:

 ' ImageIcon icon = new ImageIcon(".//Ressources//User_50.png"); this.setIconImage(icon.getImage());' 
0
Jan 26 '19 at 1:23
source share



All Articles