java in netbeans --> images don't appear

Discussion in 'Java' started by SV, Oct 20, 2004.

  1. SV

    SV Guest

    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.
     
    SV, Oct 20, 2004
    #1
    1. Advertising

  2. SV

    Tim Ward Guest


    >
    > 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.

    --
    Tim Ward
    Brett Ward Limited - www.brettward.co.uk
     
    Last edited by a moderator: Oct 4, 2009
    Tim Ward, Oct 20, 2004
    #2
    1. Advertising

  3. SV

    SV Guest

    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 ...

    > > When I try to run something graphic that contains images, the images

    never
    > > appear.
    >>


    > 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.>
     
    SV, Oct 20, 2004
    #3
  4. SV

    Tim Ward Guest


    > 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.

    --
    Tim Ward
    Brett Ward Limited - www.brettward.co.uk
     
    Last edited by a moderator: Oct 4, 2009
    Tim Ward, Oct 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    394
  2. Dom
    Replies:
    2
    Views:
    771
  3. PJ

    Controls don't appear - Newbie Question

    PJ, Jul 21, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    122
  4. Xeno Campanoli
    Replies:
    2
    Views:
    97
    Brian Candler
    Oct 31, 2008
  5. Howard Jess

    Form images don't appear in form.elements

    Howard Jess, Oct 18, 2004, in forum: Javascript
    Replies:
    1
    Views:
    173
    Richard Cornford
    Oct 18, 2004
Loading...

Share This Page