Hello....

I'm currently using Netbean 5.0 and I just want to add some GIF files to my program when it builds. I'm using the files as icons for some of the GUI buttons and labels. But when I select clean and build main project or build main project and execute the JAR file outside the IDE, the icons are missing. :sad:

I put the image files into my project folder and basically just add this kinda code in.

Icon smile = new ImageIcon("smile.gif");
label2 = new JLabel("Label with text and smiley icon", smile,
SwingConstants.LEFT);
label2.setToolTipText("This is label2");
container.add(label2);

So..I'm wondering if anyone can help me with this....

Thanks in advance.

Member Avatar for iamthwee

If you set the paths to the image files, such as:

c:/myfolder/smile.gif

-put the image file here!

Then your jar file should pick them up from wherever you execute it from.

If you set the paths to the image files, such as:

c:/myfolder/smile.gif

-put the image file here!

Then your jar file should pick them up from wherever you execute it from.

I see... hmm... I'll check it out.

By the way, are there any ways to like include the image file into the JAR file and read it from there, so that the end product is just one file. Easier for distribution I guess and it eliminates the need to change the codes everytime.

A newbie here, but I think I saw some other files that are in the JAR file, like some kinda resources that are included in it. So, I'm wondering if the images can be included in as well.

Kinda hard when you're switching from something as easy as Delphi where you concentrate more on the programming aspect rather than the GUI....hehe :o

Member Avatar for iamthwee

>By the way, are there any ways to like include the image file into the JAR file and read it from there, so that the end product is just one file. Easier for distribution I guess and it eliminates the need to change the codes everytime.

I don't think that's possible, although I'm just guessing here. The only other way out of it, is to ensure your jar file is in the same directory as your pictures. But like you say that has obvious problems. You can't execute it properly if you drag the jar file elsewhere - the pictures won't link it up.

Hmmm.

you just need to place the image/image_folder in %project_file%\build\classes.
now when you build the project it will be automativally added to the jar archive, which is now executable jar archive.

i use netbeans 6.0

sabithpocker, so that's how you'd access an image? I agree that the image can be included in the generated jar file. However, my feeling is that the file will not be accessible via the Java code since the current path (using relative addressing) would be the path the jar executable is being run from and not point to the files within the jar file. Consider:

import java.awt.*;
import java.net.*;
public class ResourceTest extends Frame {

	Image img;

	public ResourceTest() throws Exception {
		URL myurl = this.getClass().getResource("/myimage.gif");
		Toolkit tk = this.getToolkit();
		img = tk.getImage(myurl);
	}  //  public ResourceTest()

	public void paint(Graphics g) {
		g.drawImage(img, 0, 0, this);
	}  //  public void paint()

}  //  public class ResourceTest extends Frame

This code was taken from: http://www.devx.com/tips/Tip/5697 and is generally then way one would include an image loaded from an executable jar file. I believe this is what the poster was looking for? Oh, of course adding the image to your project is as simple as dragging the image and putting it into perhaps the source folder of your project (which is on the Projects tab in the NetBeans IDE). This, as the poster said, makes distribution of the project, much more robust.

A very late reply, but something someone else might find useful.

sabithpocker, so that's how you'd access an image? I agree that the image can be included in the generated jar file. However, my feeling is that the file will not be accessible via the Java code since the current path (using relative addressing) would be the path the jar executable is being run from and not point to the files within the jar file. Consider:

import java.awt.*;
import java.net.*;
public class ResourceTest extends Frame {

	Image img;

	public ResourceTest() throws Exception {
		URL myurl = this.getClass().getResource("/myimage.gif");
		Toolkit tk = this.getToolkit();
		img = tk.getImage(myurl);
	}  //  public ResourceTest()

	public void paint(Graphics g) {
		g.drawImage(img, 0, 0, this);
	}  //  public void paint()

}  //  public class ResourceTest extends Frame

This code was taken from: http://www.devx.com/tips/Tip/5697 and is generally then way one would include an image loaded from an executable jar file. I believe this is what the poster was looking for? Oh, of course adding the image to your project is as simple as dragging the image and putting it into perhaps the source folder of your project (which is on the Projects tab in the NetBeans IDE). This, as the poster said, makes distribution of the project, much more robust.

A very late reply, but something someone else might find useful.

i did java code long before that i dont remmeber exactly what all i did and i am not sure of what PoovenM is tryin to convey.
As i understood from his reply he only need to address to a location inside jar file
new URL( "jar:file:/" + i_jarFileName + "/!" + "/" + "icon.gif" )

or a similar path can refer to files inside jar file....once you have compiled the image to jar file use such path and if there is any more problem please reply i might refer to my old codes again :D
(my goal at that time was to include every file in the single jar file like what we have in windows portable executive, i dint want any image to be outside jar)

yes, using getResource or better yet getResourceAsStream is the way to extract resources from a jarfile.
Make sure that those resources are contained in the jarfile as part of the classpath or you'll get an error indicating that the resource doesn't exist.
That means that during compilation (and jarring) the files need to be copied so they exist next to the classfiles for the application.

hello
I want add G.src.jar to netbeans6.5
can you help me?????????????

Right click on the project >> select PROPERTIES. In the pop-up window, in CATEGORIES (left side listing) select Libraries & Resources >>on the right press Add JAR/Folder and navigate to place where is the driver or any other library you wish to add >> highlight file/s you want to include and press OK.
You can see sample here

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.