This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 62844
203
  • 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.
 
Rancher
Posts: 3353
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: 3000
20
  • 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
Rancher
Posts: 3353
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  
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!