67

I'm using NetBeans, trying to change the familiar Java coffee cup icon to a png file that I have saved in a resources directory in the jar file. I've found many different web pages that claim they have a solution, but so far none of them work.

Here's what I have at the moment (leaving out 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 that contains this code is in the com.xyz package, if that makes any difference. That class also extends JFrame. This code is throwing a MalformedUrlException on the first line.

Anyone have a solution that works?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • See also: http://java.sun.com/javase/6/docs/api/java/awt/Window.html#setIconImages(java.util.List) http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.net.URL) – McDowell Oct 16 '08 at 19:12

10 Answers10

81
java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png");

May or may not require a '/' at the front of the path.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
JeeBee
  • 17,476
  • 5
  • 50
  • 60
  • 41
    Thanks. This is a perfect example of why we need StackOverflow. I found 100 different "solutions" by googling before I posted this question and got an answer in 5 minutes. – Bill the Lizard Oct 16 '08 at 19:05
  • 6
    It's pretty safe to put the "/" in front. Also, it lets you take the same string and do a getResourceAsStream, which is sometimes more useful. – Daniel Spiewak Oct 16 '08 at 19:09
  • 1
    Wow--the power of StackOverflow! :-) – Onorio Catenacci Oct 16 '08 at 19:11
  • Glad to help - I'd just not one day beforehand had to do something similar :) – JeeBee Oct 17 '08 at 11:27
  • 1
    And where is the png file actually stored? – CodyBugstein Jan 20 '14 at 09:55
  • 1
    JAR/classes/com/xyz/resources/camera.png But you should be using Maven to manage your project and build, so in code it would be PROJECT/src/main/resources, and Maven will copy it into the correct place in its Jar/War creation phase. – JeeBee Sep 26 '14 at 10:54
14

You can simply go Netbeans, in the design view, go to JFrame property, choose icon image property, Choose Set Form's iconImage property using: "Custom code" and then in the Form.SetIconImage() function put the following code:

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

Do not forget to import:

import java.awt.Toolkit;

in the source code!

tbodt
  • 16,609
  • 6
  • 58
  • 83
Ayoub Aneddame
  • 141
  • 1
  • 2
4

Try This write after

initcomponents();

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));
pb2q
  • 58,613
  • 19
  • 146
  • 147
4

Or place the image in a location relative to a class and you don't need all that package/path info in the string itself.

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

That way if you move the class to a different package, you dont have to find all the strings, you just move the class and its resources directory.

John Gardner
  • 24,225
  • 5
  • 58
  • 76
  • If you use a relative path and then create a subclass in a different package, that path will no longer be valid and your code will break. Using absolute paths prevents this (e.g. "/com/xyz/resources/camera.png") – hohonuuli Nov 02 '10 at 18:28
2
    /** 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 GUI in netbeans and it worked perfectly

2

You should define icons of various size, Windows and Linux distros like Ubuntu use different icons in 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);
ron190
  • 1,032
  • 1
  • 17
  • 29
1

In a class that extends a javax.swing.JFrame use method setIconImage.

this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage());
user2601995
  • 6,463
  • 8
  • 37
  • 41
0

You can try this one, it works just fine :

`   ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
    this.setIconImage(icon.getImage());`
-1

inside frame constructor

try{    
       setIconImage(ImageIO.read(new File("./images/icon.png")));   
   }
catch (Exception ex){
       //do something
   }
Alex S
  • 66
  • 5
-2

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);
ron190
  • 1,032
  • 1
  • 17
  • 29