• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Adding Icons to JFrame  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some assistance figuring out how to add an icon to a JFrame. I have the following code, but am unable to get it to work due to issues with line three. Does anyone in the community have any ideas what I'm doing wrong or if I completely missed something? Thanks in advance!
 
Marshal
Posts: 59762
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you usually use a JPanel and set the icon on that. Then add the panel to the frame, remebering what sort of layout the frame has as a default. Unless the icon is tiny, it is probably best to wrap the panel in a JScrollPane. Have a look at these two sections of the Java™ Tutorials:- 1 2.
 
Master Rancher
Posts: 3268
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works for me.  It places the selected image in the left hand side of the title bar.

issues with line three


Please explain.
 
Rancher
Posts: 2934
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it doesn't work then you probably aren't reading the Image.

Use ImageIO.read(...) to read the image. You will get an error message if it can't find the image file.
 
Paul Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I figured it out (please correct my if my terminology is wrong - I'm new to Java and OOP, but definitely want to improve my terminology). The issue (particularly as related to line 3) is that I was adding the ImageIcon object with the global variables and then instantiating the frame's to use that for an icon all in the same location. Once I instantiated the object (icon) in the constructor, the frame applied the icon. I'll provide a snippet of the code below in case anyone in the future has a similar issue.

Campbell, I'm definitely going to try the JScrollPane now that I've got it working. Thanks for the help guys!

 
Norm Radder
Master Rancher
Posts: 3268
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the issue was the compiler was giving an error message for line 3.  The message implied that the code on line 3 must be inside of a method.  The new code has moved line 3 to line 8 inside of a method.
It would help if you would had copied the full text of the error message and pasted it here.
 
Paul Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm, yes - exactly. And thanks for the heads-up; next time I'll be sure to post the error message(s) that so joyfully accompany my ineptitudes 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!