Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding an Image to a Panel

 
Ben Jass
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a pretty basic question. How do I add an image (for example: title.png), to a panel?

I mean, I wish I could just do this:

 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLabel has a constructor which accepts an image.
You can constructor a JLabel with the image and then add the label to the parent.
 
Ben Jass
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This didn't work, nothing showed up.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the path used for the ImageIcon is not correct- why is a "cd" there in the path?
 
Ben Jass
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know where to put the pictures so saying "cd Desktop" just like in the cmd line brings you to the desktop. I use packages so where would be the best place to put it if my packages are com.divinesouls.firstscreen
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the images in the same file as the class files, then use getClass().getResource("Tic Tac Toe Title.png") to get a URL you can use in the ImageIcon constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic