• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding an image to a frame

 
rohith yenumula
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@miguel, good example, now you are getting the hang of a SSCCE.
 
rohith yenumula
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rohith yenumula
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohith, ItDoesntWorkIsUseless. As to the "DisplayImage$1" class, read up on anonymous classes.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turn the line just under line 10 into the following, and you will see what file it is trying to read:
 
rohith yenumula
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic