Results 1 to 6 of 6
  1. #1
    JaiRaj is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default ImageIcon won't load my image

    My image won't display at all with the following code, I've tried numerous times to make sure the image is in the right place and trying different images. The code below is the constructor for my class which extends JFrame. Does anyone know what my problem might be?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    public class ImageThing {
    
    	public static void main(String[] args)
    	{
    		new ImageThingie();
    
    	}
    	
    }
    
    class ImageThingie extends JFrame
    {
    	public ImageThingie()
    	{
    		 this.setPreferredSize(new Dimension(400, 600));
    	         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	         
    	         JPanel pTop = new JPanel();
    	         ImageIcon image = new ImageIcon(getClass().getResource("MenuImage.jpg"));
    	         pTop.add(new JLabel(image));
    	         
    	         this.add(pTop);
    	         this.pack();
    	         this.setVisible(true);
    	}
    }

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If you use a String for your JLabel instead of an ImageIcon, does that work? If so, and assuming you are not getting any exceptions, then it may be that the image dimensions are nonsensical, or the image has an alpha value of 255 everywhere, or other crazy things weird with image.

    If using a String for the JLabel also doesn't work, then probably the JRE is an earlier version of swing. In earlier versions of swing, you had to do this:

    Java Code:
    this.getContentPane().add(...)
    Finally, you may wish to choose a layout manager rather than using the default, which I think is a BorderLayout.

    (also, you can try ClassLoader.getSystemClassLoader().getResource() ...there are some subtle differences between this and what you are doing)
    Last edited by toadaly; 03-03-2010 at 07:30 AM.

  3. #3
    ziaf is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    well, i think it is issue with the image.
    My understanding is that .jpg wont work with java.
    can u take that image in mspaint and save as gif. then try again.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    JPEGs *do* work with ImageIcon. It supports png, jpg, and gif for files/urls.

  5. #5
    JaiRaj is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Well I ended up getting it to work, it seems the default location for finding the images for Eclipse is in the overall project folder rather than where the class files are. This still doesn't explain how it didn't load an image that was directly inside the C: drive

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If it couldn't find the resource, it should have thrown an exception. You would have been able to find your problem very quickly if you are handling exceptions properly.

Similar Threads

  1. How to insert Image using ImageIcon
    By anjali.wadhwa in forum New To Java
    Replies: 5
    Last Post: 01-16-2010, 12:08 AM
  2. ImageIcon : Image not displayed
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 08:36 AM
  3. [SOLVED] Load image into a JSP
    By jazz2k8 in forum New To Java
    Replies: 0
    Last Post: 05-08-2008, 12:33 PM
  4. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 07:21 AM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:16 AM

Posting Permissions

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