This is driving me crazy. I downloaded netbeans and love it but for the last 5-6 hours i have been trying to figure out why my java programs is not showing images.
When i open it with jGrasp the images and everything loads perfect.
With netbeans it will not show any images. I have been going at this for a long time now and desperaptly need help.
The way I have it working in jGrasp is like this

jcomp10 = new JButton();
          ImageIcon under = new ImageIcon("image/w2.png");
          JButton jcomp10 = new JButton(under);

my other image in my program i used it like this

final ImageIcon icon2 = new ImageIcon("image/java2.png");
JOptionPane.showMessageDialog(null,"blah blah", "blah blah", + 
                        JOptionPane.INFORMATION_MESSAGE, icon2);

in net beans i have tried to make new packages called resources and images and put the images in there and changed it to "resource/java2.png" and tried to put it in the same folder as the .java file and switched it to just "java2.png"
Don't know why its not working.
Im a complete noob at programming (started 4 weeks ago) so if you know why I'm failing
please try to explain it so i can understand it easy.
Thanks guys and hopefully you guys can help me
this is driving me crazy!!!
If you guys can't help ill probably go outside and punch a tree for a few hours. lol

Recommended Answers

All 9 Replies

90% probability that it's just looking in the wrong place.
You can try
System.out.println(new File("xxx").getAbsolutePath());
(with your own file names) to see exactly where Java is expecting to find them.

System.out.println(new File("xxx").getAbsolutePath());

Im a noob. how exactly and where do I put

System.out.println(new File("xxx").getAbsolutePath());

Where it will be executed. First thing in the main() method would be a good spot.

ok so it returned this
/Users/bnproductions/ALL/333TESting still/FinalProject/w2.png

so i tried this

menuPic = new JButton();
ImageIcon bottompic = new ImageIcon("/Users/bnproductions/ALL/333TESting still/FinalProject/w2.png");
JButton menuPic = new JButton(bottempic);

Its still not displaying any image.
Ive spent countless hours trying to figure this out.
is there another code i can try to make the button an image?

System.out.println(new File("xxx").getAbsolutePath());

ok so i got it to work with netbeans but now it doesn't work in JGrasp.
Is there another way to write this so it works with JGrasp.

The working code for netbeans is

menuPic = new javax.swing.JButton();
menuPic.setIcon(new javax.swing.ImageIcon(getClass().getResource("image/w2.png")));

and the working code for JGrasp is

menuPic = new JButton();
ImageIcon bottompic = new ImageIcon("image/w2.png");
JButton menuPic = new JButton(bottompic);

I need a code that will work for both programs. I hate JGrasp but thats what my teacher uses to grade, so I want to be able to write it in netbeans and copy and paste the code to JGrasp and it be able to work.
Any help would be greatly appreciated!!

What is the difference between the paths that are printed out in the two places you execute the code?

The getResource() method will search on the classpath.
The other uses the current directory.

Both techniques should work depending on where you put the image file. Can you expain the folder locations for the files, the current folder and the classpath.

So i added this to both projects

System.out.println(System.getProperty("user.dir"));   // Show the current directory
System.out.println(System.getProperty("java.class.path"));  // show the classpath
System.out.println(new File("picLocation").getAbsolutePath());

JGrasp returned this

/Users/bnproductions/ALL/222Project files


/Users/bnproductions/ALL/222Project files/picLocation

netbeans returned this

/Users/bnproductions/ALL/333TESting still/FinalProject
/Users/bnproductions/ALL/333TESting still/FinalProject/build/classes
/Users/bnproductions/ALL/333TESting still/FinalProject/picLocation

and now I'm trying eclipse and it returned this


This just confused me 10x more.
With jGrasp i just added a new folder called image in the same location as the .java file
and this works perfect

ImageIcon bottompic = new ImageIcon("image/w2.png");

JButton menuPic = new JButton(bottompic);

i tried to do the same thing in netbeans and eclipse but i get nothing.
I'm sooooo confused now

got it figured out just did

ImageIcon bottompic = new ImageIcon("/Users/bnproductions/ALL/123FinalProject/bin/w2.png");
JButton menuPic = new JButton(bottompic);

still confused by it all but i got it now.
even though i made a new package called image and put my image files there, it looks in /bin for the images

You can see from the print outs that all the paths are different. That means that if you use getResource() it will look in one folder and if you use a relative path to the file it will look in another location.
Each IDE sets the paths to different places. You have to locate the image files for the IDE that you are using.

If you are going to put the program and image files in a jar file, use the getResource() method and copy the image files to where the IDE sets the classpath.

Be a part of the DaniWeb community

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