Results 1 to 6 of 6
  1. #1
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default Showing an image with JLabel and ImageIcon - so simple but doesnt work!

    hi guys, here's my short working code

    Java Code:
    import java.awt.Dimension;
    import javax.swing.*;
    
    public class test {
    	public static void main (String[] args) {
    		JFrame frame = new JFrame();
    		JPanel panel = new JPanel();
    		
    		JLabel pic = new JLabel();
    		pic.setIcon(new ImageIcon("default.jpg"));
    		
    		panel.add(pic);
    		panel.setPreferredSize(new Dimension (200,200));
    		
    		frame.getContentPane().add(panel);
    		frame.pack();
    		frame.setVisible(true);
    		
    	}
    }
    if you want to put the image with it, mine's attached, or use your own. There is an example in my book just like this but mine won't work...

    I have that image in the same folder as my .java file which I think is how it's supposed to be. Even with an absolute path I have no luck.

    Thanks!
    Attached Thumbnails Attached Thumbnails Showing an image with JLabel and ImageIcon - so simple but doesnt work!-default.jpg  

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I have that image in the same folder as my .java file

    Your code works for me.

    The most likely explanation is that the ImageIcon constructor is not finding the image file (as you suggest).

    You might get an idea of what is going on by printing out some detail just before you invoke the ImageIcon constructor:

    Java Code:
    try {
        System.out.println(
                "About to load " + imgF.getCanonicalPath() 
                + " exists=" + imgF.exists());
    } catch(IOException ioe) {
        ioe.printStackTrace();
        System.exit(0);
    }
    pic.setIcon(new ImageIcon("default.jpg"));

    (You will need to import java.io.IOExceoption and java.io.File)

    Since your icon isn't loading and mine (when I run your code with your image) *is*, it would seem that there might be something else about how you are running the code that is getting in the way. IDE or whatever - something you haven't said.

    (Use forward slashes for the file name if you are specifying the path explicitly. And a better name for the class would be Test, or ImageIconTest.)

  3. #3
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    and what is imgF ? an image file?

    I am using eclipse and will give the forward slashes, plus what you said a try (I'll try and figure out what imgF should be)

    I shouldn't need to specify an absolute path though if the image is in the same folder as the .java file correct?

    thanks for the help

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Sorry, I didn't include that line!

    Java Code:
    import java.awt.Dimension;
    import javax.swing.*;
    import java.io.File;
    import java.io.IOException;
    
    public class test {
        public static void main (String[] args) {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
    		
            JLabel pic = new JLabel();
            File imgF = new File("default.jpg");
            try {
                System.out.println(
                        "About to load " + imgF.getCanonicalPath() 
                        + " exists=" + imgF.exists());
            } catch(IOException ioe) {
                ioe.printStackTrace();
                System.exit(0);
            }
            pic.setIcon(new ImageIcon("default.jpg"));
    		
            panel.add(pic);
            panel.setPreferredSize(new Dimension (200,200));
    		
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
    		
        }
    }

  5. #5
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    nice debugging dude! The output

    Java Code:
    About to load D:\work\school\CS 1331\homework\hw10\default.jpg exists=false
    told me the pic should be in the project folder not the source folder!

    Thanks - will remember this debugging.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Last JLabel I add to JPanel doesnt show up
    By Ambergahill in forum New To Java
    Replies: 6
    Last Post: 01-29-2014, 09:07 PM
  2. ImageIcon doesnt work after exporting the project
    By random7 in forum New To Java
    Replies: 8
    Last Post: 07-27-2010, 10:00 PM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  4. PrintWriter doesnt work :(
    By Addez in forum New To Java
    Replies: 11
    Last Post: 01-17-2010, 06:59 PM
  5. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 02:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •