ImageIcon not showing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scaran54
    New Member
    • Jan 2010
    • 3

    ImageIcon not showing

    Hello, I am following a tutorial but have a problem.
    With the following code the icons will not appear unless I specify the entire path to where they are located, I cant work out why it wont work, what am I missing?

    Thanks for any help

    Code:
    public class IconFrame extends JFrame {
        JButton load, save, subscribe, unsubscribe;
    
        public IconFrame() {
            super("Icon Frame");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            // create icons
    
            ImageIcon loadIcon = new ImageIcon("load.gif");
            ImageIcon saveIcon = new ImageIcon("save.gif");
            ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
            ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
            // create buttons
            load = new JButton("Load", loadIcon);
            save = new JButton("Save", saveIcon);
            subscribe = new JButton("Subscribe", subscribeIcon);
            unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
            // add buttons to panel
            panel.add(load);
            panel.add(save);
            panel.add(subscribe);
            panel.add(unsubscribe);
            // add the panel to a frame
            add(panel);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] arguments) {
            IconFrame ike = new IconFrame();
        }
    }
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    The Java runtime has to be told where the file is!

    Code:
    ImageIcon loadIcon = new ImageIcon("load.gif");
    This will attempt to find the image in the "current working directory" - the directory from where the java executable was launched. Depending on where you place the image file it is not actually true that you must specify the full path.

    Perhaps you could say what you are trying to do. Ie explain where the image files are placed with respect to the other files that make up the application.

    A common approach is to place load.gif in the same directory as IconFrame.class or in the same directory as some other class file. Then you can use the Class methods getResource() or getResourceAsSt ream(). Notice that ImageIcon has a constructor that takes a URL argument which can be supplied by the getResource() method. This approach has the advantage that only relative paths need be used, and it will work when the application (.class and image files) are within a jar archive.

    Comment

    • Scaran54
      New Member
      • Jan 2010
      • 3

      #3
      Hello,

      I managed to fix the problem in the end by changing the PATH variable in system settings after quite alot of mild annoyance over something that I thought should have just worked.

      Thanks very much for your response!

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        I'm glad you've got what you want.

        However the operating system's (or shell's) PATH variable controls the directories within which it (the OS or shell) will look for executable files: something which has nothing to do with resource location within a Java (or any other) application.

        Comment

        • Scaran54
          New Member
          • Jan 2010
          • 3

          #5
          This will attempt to find the image in the "current working directory" - the directory from where the java executable was launched. Depending on where you place the image file it is not actually true that you must specify the full path.
          Before I changed the PATH file I had the images in the "current working directory" but it did not find them. I changed the PATH to the folder that holds my projects, is that correct?

          Which class supplys the getResource() and getResourceAsSt ream() methods?

          Thanks

          Comment

          • pbrockway2
            Recognized Expert New Member
            • Nov 2007
            • 151

            #6
            Originally posted by Scaran54
            Before I changed the PATH file I had the images in the "current working directory" but it did not find them. I changed the PATH to the folder that holds my projects, is that correct?
            Honestly I can't see how the images would not be found if they were in the same directory as that from which the application was launched. Regardless of the setting of the OS PATH variable.

            But perhaps I'm missing something. One way to tell would be to post a small example of what you mean: something that prints the cwd with System.out.prin tln(System.getP roperty("user.d ir")), reports the file's existence with new File("load.gif" ).exists(), creates the icon as you do now and then reports the value of loadIcon.getIma ge().

            Originally posted by Scaran54
            Which class supplys the getResource() and getResourceAsSt ream() methods?
            They are in the Class class. (If you haven't used it before there is an index) page as part of the javadoc.)

            You might use them like:

            Code:
            url loadURL = IconFrame.class.getResource("load.gif");
            ImageIcon imageIcon = new ImageIcon(loadURL);
            This code assumes that load.gif is placed in the same directory as IconFrame.class .

            You might find the code in the How to Use Icons chapter of Sun's Tutorial useful: they describe how to load an icon, again from a location that is relative to the class doing the loading. (They use another Class method - getClass() - instead of using a class literal like ImageIcon.class .)

            Comment

            Working...