java in netbeans --> images don't appear

S

SV

Hello,

I just installed Netbeans, and everything works fine except one thing:

When I try to run something graphic that contains images, the images never
appear.
Allthough when I run the same program in DOS, everything works fine! What I
am doing wrong?

I am sure that the exact same code works fine in DOS, and that the image is
really in the correct folder.
 
T

Tim Ward

I just installed Netbeans, and everything works fine except one thing:

When I try to run something graphic that contains images, the images never
appear.
Allthough when I run the same program in DOS, everything works fine! What I
am doing wrong?

I am sure that the exact same code works fine in DOS, and that the image is
really in the correct folder.

If the method you use to locate the image depends on any concept of "current
directory" be aware that NetBeans tends to set this to something completely
useless and unexpected for no obviously good reason. (Applications *should*
*not* mess with the current directory *ever* - it belongs to the user, and
only the user should get to change it. For some reason the NetBeans people
won't acknowledge that this is a bug.)

The solution is to choose a different method of locating the image file, one
that doesn't depend on the "current directory" method. Of course, you're
likely to have chosen some relative path means of navigation so as to avoid
the even worse use of absolute paths ...

Look up the various bits of

Toolkit.getDefaultToolkit().getImage( MyApplication.class.getResource(
"myicon.gif" ) )

and related stuff until you manage to come up with a similarly
incomprehensible magic incantation that does what you want. I think the
above works because "MyApplication.class" and "myicon.gif" are in the same
directory, but the details of this gibberish are so horrendous that I
deliberately forgot them as soon as I got the code working.
 
Last edited by a moderator:
S

SV

The image file and the java file are in the same directory, so I just do
cp.add(new JLabel(new ImageIcon("image.gif")));
I don't want to use different methodes etc. for that because it 's a project
for school so it has to look like it 's not written especially for NetBeans
....
So I 'll either have to find a solution, or look for a different IDE ;-)
Right now I 'm editing the code in NetBeans, and testing in DOS, but that 's
a shame because all NetBeans' functions for compiling and running are not
used ...
 
T

Tim Ward

The image file and the java file are in the same directory, so I just do
cp.add(new JLabel(new ImageIcon("image.gif")));
I don't want to use different methodes etc. for that because it 's a project
for school so it has to look like it 's not written especially for
NetBeans

No, the type of solution I outlined is *not* specific to NetBeans, it's how
you are *supposed* to do this sort of thing "properly". Your own code would
quite possible fail if you were in a different current directory when you
ran it from the command line completely independently of NetBeans - try it.
 
Last edited by a moderator:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top