Hello dear codegurus!
I've been working on a pet project of mine for 3 weeks now, and I think it is ready to be released into the open!
However, just for cosmetic purposes, I would LOVE to add an icon (both for the application and in the upper left corner while running).

Is there a way to add the icon INTO the .jar file? My whole application is one single .jar file and it would be stupid if people had to copy 2 files just to see an icon. If yes, could I trouble you for a push in the right direction?
I've googled around and I've found some ways, but none worked for me. :/
Most had one thing in common - their location in code!

    public MainWindowGUI() {
        initComponents();
        // RIGHT HERE!  
    }

Thanks for reading this,
Pobunjenik

PS
Sorry for not writing for a while, I've been super busy with school and this new puppy I got (will provide pictures if requested).

PPS
The application I'm talking about is a simple hit chance calculator for the popular MMO EVE Online.
Will link completed application after adding the icon!

Recommended Answers

All 10 Replies

why not?
here you'll find how to use an image that's stored in the jar file.

Hi Pob, welcome back!
You can use something along the lines of

BufferedImage image = 
     ImageIO.read(getClass().getResource("/images/xxx.png"));
myMainWindow.setIconImage(image);

wher /images is a folder in the same jar that the current class was loaded from

Alright! Thanks for the feedback, I've tried James's code and I got the same error as the guy in Stultuske's link.
Working on it!

I use that code in many programs without problems. Can you show the actual contents of your jar file (use any zip utility to open it)?

Well, turns out using .ico instead of .png throws a NullPointerException error.
Also, I didn't know that the .png's had to be placed in myProjectName/build/classes/resources/picture.png.

Your code workes. It was I who caused the error(s). :D
Whoever might read this in the future, you have to use a try/catch block.
Like so:

try {
    Image icon = ImageIO.read(getClass().getResource("/resources/nameOfIcon.png"));
    this.myButton1.setIcon(new ImageIcon(icon));
} catch (IOException ex) {
    Logger.getLogger(MainWindowGUI.class.getName()).log(Level.SEVERE, null, ex);
}

I have just finished adding all the icons!
It took quite some time to resize and code over 40 icons.

And then I clicked "Clean and build"...
Now the resource folder is gone and I have to redo the whole thing. :@

I've tried with just two icons; and I can't get the jar to run outside of the IDE (NetBeans 7.4). What am I doing wrong? :(

Without more info its hard to say, but ...
The contents of the build folder are replaced every time you rebuild the project - you should never put anything there yourself.
You should put the resources folder inside the source packages folder of your project (at the same level as your top-level packages). NetBeans will do the rest.

Funnily enough I had the try/catch in the code when I first posted it, but then I thought that would obscure the real point, and assumed you would sus out the exception handling anyway :(

I've had no time to work the code over the weekend.
I'll try stuff tomorrow and then I'll report back.

I have only 1 package with 7 classes. In the project folder I've got these folders:
- build
- dist
- lib
- nbproject
- src
- test
and files:
- manifest.mf
- build.xml

Now, if I understood your instructions: I am to make a resource folder in the same directory where those 6 folders are, right?

Here's a screenshot from my NetBeans projects browser showing an images folder for one of my projects, placed alongide the java source code packages...

Just add your resouces folder like that, using the Project browser, then leave it to NetBeans to do the rest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.