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", +
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
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.
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();
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!!
System.out.println(System.getProperty("user.dir")); // Show the current directory
System.out.println(System.getProperty("java.class.path")); // show the classpath
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.