Netbeans refuses to show the jpg...
Hi!
I do not know which forum is good for this problem, so I try this one...
The code below works well when executed by foot (...java showPic...).
In the NetBeans 3.6 the frame refuses to show the pic, only an emty frame!
Is it a bug?
If anyone can help, it would be very nice ;-)
import java.awt.*;
import java.awt.event.*;
publicclass showPicextends Frame{
Image pic;
public showPic(){
addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
pic = getToolkit().getImage("lemmer.jpg");
setSize(200,100);
setVisible(true);
}
publicvoid paint(Graphics g){
if (pic !=null){
g.drawImage(pic,60,20,this);
}
}
publicstaticvoid main( String[] args ){
new showPic();
}
}
[1916 byte] By [
caroditaa] at [2007-10-1 1:20:45]

Obviously NB does not find the location of the jpg-file. It resides within the same dir as the class-file. Is that wrong.
I have moved to NB 4.0, and it requires that you specify a runtime classpath for files like the jpg.
Two possibilities for 3.6 are
1. Make sure that you have mounted the directory that the jpg is in (that exact directory)
2. Check the NB program help for information about classpath, especiall runtime classpath.
I had the same problem as yours, some guys here told me to use the absolute path, but this is some thing not convinent, althoug it works. Please if you found what you are looking for let me know.with regardstleis1
Hi again
I used the NB Form Editor and added a button with Image there. then read the source code and found this :
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/button1.gif")));
so I suppose that instead of using
pic = getToolkit().getImage("lemmar.jpg");
use
pic = getClass().getResource("lemmar.jpg");
and I guess you need to import javax.swing.* too
> Obviously NB does not find the location of the
> jpg-file. It resides within the same dir as the
> class-file. Is that wrong.
I tried your program and to get it to work in NB 4.0 I had to set the working directory parameter to the jpg directory path.
I don't remember if 3.6 has an equivalent parameter, you might use the program help's search and see what you find.
Unfortunatly It didn't work for me
I have the following:
ImageIcon image1 = new ImageIcon("images/button1.gif");
I can run the application, but I can't view the image
Changing it to :
ImageIcon image1 = new ImageIcon(getClass().getResource("images/button1.gif"));
Now I can compile it correctly, but I can't execute ( exception in thread "main") ! Why?
By the way, sometimes when I compile in NetBeans I got Errors while compiling (FileName). In this case I open the command line and run javac (and it works fine)!!! WHY?
Hi!
Thanx to you all for your help. Now I proudly present the workarounds
In NB3.6 it is done by supplying the fully qualified file name
pic = getToolkit().getImage("e:/1 1/lemmer1.JPG");
Please note spaces in the path...
In NB4.0 it is done by supplying a "working directory"
Properties of the project showPic -> Run - > Running Project -> Working directory
getClass().getResource didn't work for me
> In NB3.6 it is done by supplying the fully qualified
> file name
Yes, that is a solution if you're willing to hardcode the fully-qualified name. The drawback is that portability is lost.
> In NB4.0 it is done by supplying a "working
> directory"
Again, portability problems.
> getClass().getResource didn't work for me
This works if the resource is on the classpath, and preserves portability. However, finding a location that NB considers "on the classpath" is sometimes a case of trial and error.