Results 1 to 6 of 6
Thread: ImageIcon won't load my image
- 03-03-2010, 06:13 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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); } }
- 03-03-2010, 06:28 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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:
Finally, you may wish to choose a layout manager rather than using the default, which I think is a BorderLayout.Java Code:this.getContentPane().add(...)
(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 06:30 AM.
- 03-03-2010, 07:15 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
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.
- 03-03-2010, 07:10 PM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
JPEGs *do* work with ImageIcon. It supports png, jpg, and gif for files/urls.
- 03-04-2010, 04:08 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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
- 03-04-2010, 06:35 PM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
How to insert Image using ImageIcon
By anjali.wadhwa in forum New To JavaReplies: 5Last Post: 01-15-2010, 11:08 PM -
ImageIcon : Image not displayed
By niteshwar.bhardwaj in forum Java 2DReplies: 0Last Post: 02-13-2009, 07:36 AM -
[SOLVED] Load image into a JSP
By jazz2k8 in forum New To JavaReplies: 0Last Post: 05-08-2008, 11:33 AM -
copy image/imageicon into a file on disk
By archanajathan in forum Advanced JavaReplies: 2Last Post: 11-22-2007, 06:21 AM -
Help with load image
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks