Results 1 to 6 of 6
- 12-03-2010, 11:07 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
Showing an image with JLabel and ImageIcon - so simple but doesnt work!
hi guys, here's my short working code
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...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); } }
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!
- 12-03-2010, 11:36 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.)
- 12-04-2010, 12:11 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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
- 12-04-2010, 12:14 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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); } }
- 12-04-2010, 12:24 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
nice debugging dude! The output
told me the pic should be in the project folder not the source folder!Java Code:About to load D:\work\school\CS 1331\homework\hw10\default.jpg exists=false
Thanks - will remember this debugging.
- 12-04-2010, 12:30 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Last JLabel I add to JPanel doesnt show up
By Ambergahill in forum New To JavaReplies: 5Last Post: 11-15-2010, 10:37 PM -
ImageIcon doesnt work after exporting the project
By random7 in forum New To JavaReplies: 8Last Post: 07-27-2010, 09:00 PM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
PrintWriter doesnt work :(
By Addez in forum New To JavaReplies: 11Last Post: 01-17-2010, 05:59 PM -
[SOLVED] JLabel not showing on JPanel
By onefootswill in forum New To JavaReplies: 11Last Post: 08-23-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks