• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Adding an image to a frame

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add image to JFrame
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rohith, ItDoesntWorkIsUseless. As to the "DisplayImage$1" class, read up on anonymous classes.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic