aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding an image to a frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding an image to a frame" Watch "Adding an image to a frame" New topic
Author

Adding an image to a frame

rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Hi. I want to add an image to a frame. I want to know how one creates an image object and displays it in a frame.

Thanks in advance.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

add image to JFrame

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
rohith yenumula wrote:Hi. I want to add an image to a frame. I want to know how one creates an image object and displays it in a frame.

Thanks in advance.

you need a JLabel to display the image, and then you add it to a JPanel


java amateur
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
@miguel, good example, now you are getting the hang of a SSCCE.
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Thanks for the example.

label.setIcon(new ImageIcon("myImage"));


I have a doubt regarding the ImageIcon constructor. I tried passing the relative and absolute address of a image but there was no image displayed.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
rohith yenumula wrote:Thanks for the example.

label.setIcon(new ImageIcon("myImage"));


I have a doubt regarding the ImageIcon constructor. I tried passing the relative and absolute address of a image but there was no image displayed.

your problem is setting the relative path
try this:
1) save your image next to your your file (then the path i gave you is the correct)
2) save your image inside a folder called img and put this folder next to your file (then the path is: img/"myImage")
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Hi Miguel. The name of my image is "rf" with extension JPEG. The image is in the same folder as the DisplayImage file.

I passed the name as follows but it doesn't work.

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
java is case sensitive
so, JPEG is diferent from jpeg, i guess
also, you have to give the extension and also all that should be inside quotes ("myImage.png")

as a side note, you should always pay atention to the compiler error messages - the compiler is your friend
try this and let me know if it works:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

miguel lisboa wrote:java is case sensitive
so, JPEG is diferent from jpeg, i guess

It is, until it goes to the file system (like here). Then it is as case sensitive as the file system; for Windows that means it is not case sensitive, for Linux it usually is.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Hi Miguel. I have tried doing it as you told but it didn't work.
There are no compile-time or run-time errors but two .class files(DisplayImage and DisplayImage$1) were created when I ran the program. Do you have any idea why is it so?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
rohith, ItDoesntWorkIsUseless. As to the "DisplayImage$1" class, read up on anonymous classes.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
rohith yenumula wrote:Hi Miguel. I have tried doing it as you told but it didn't work.
There are no compile-time or run-time errors but two .class files(DisplayImage and DisplayImage$1) were created when I ran the program. Do you have any idea why is it so?

post all the code you have so far, so that we give it a look
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Please don't. Most likely that will be a lot of code, and we probably don't need to view it all. And after all the answer to that question is quite easy.
DisplayImage.class is the class file for the DisplayImage class. DisplayImage$1 is the class file for the first anonymous inner class encountered inside the DisplayImage class. If you have taken Miguel's example code then that is the Runnable created in the main method.

Try to add the following instead:
This way you can see which file is being loaded (or at least tried), and if that file exists.
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Hi Rob. It is not a lot of code. It is Miguel's code.
I am new to files.
So could you tell me where to insert the statements you told.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I would say before line 11. But are you telling us you are still using the string literal "myImage"? Is that also the actual file name of your image?
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Oh sorry. The name of the image is "rf.JPEG". The entire code is as under:

The above code compiled and ran without any errors. The output generated was:
rf.JPEG
false


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Turn the line just under line 10 into the following, and you will see what file it is trying to read:
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Hi Rob. What you have been saying is right.
It was my mistake. I got the extension wrong:.JPEG instead of .JPG.
Next time I will be more careful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding an image to a frame