DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004

    Using ImageIcons in java applets


    I'm new to this site because I'm having a problem changing one of my java swing applications to an applet so that I can put it on my website. The application is a little poker game I made, and it works great (even as an applet) except for when I run it using a web browser. No matter what I do, it seems that I cannot load the images into my program running it from internet explorer. The images are .jpgs and are in a folder one up from the .class location; I am loading the images using the ImageIcon class, and setting the icons onto buttons. The weird thing is that it all works when I run the applet in my compiler, so I don't know what's wrong. ANY help will be greatly appreciated! Thank you very much!

    Babak Rashidi

  2. #2
    Join Date
    Jul 2004
    No one has any answers?

  3. #3
    Join Date
    Feb 2004
    youre attempting to read the images directly off disk, rather than usint getDocumentBase(), and hence youre running into a security exception, because applets arent allowed to access the local disk

    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Jul 2004

    Thanks for your help!

    Wow, you guys had quite the post about it before, thanks a lot! I've made the change, and my problem is better than it used to be (the program now doesn't stop running) except I still have ONE problem, that being that the images still don't show up. Anyway, it looks like it's a problem with where the images are stored or something (Although I know that's not it) but I can figure it out (and if I can't... I'll be back!) And btw cjar, your tutorial on jar files is awesome!!! I recommend anyone trying to put things on the internet to have a look!! I'm actually hoping that may solve my present problem. Thanks!


  5. #5
    Join Date
    Aug 2004
    I had difficulty with using imageicons in an applet, so I nicked the following code from Sun:

    protected static ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = SwingApplet5.class.getResource(path);
    if (imgURL != null) {
    return new ImageIcon(imgURL, description);
    } else {
    System.err.println("Couldn't find file: " + path);
    return null;

    Then in the method that uses the imageicon, I used:

    ImageIcon copyIcon = createImageIcon("images/copy.gif", "Copy");

    where the images folder is in the folder containing the applet.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.