The moose likes Swing / AWT / SWT and the fly likes Adding an Image to a Panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding an Image to a Panel" Watch "Adding an Image to a Panel" New topic

Adding an Image to a Panel

Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
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

Joined: Jul 26, 2007
Posts: 10170

JLabel has a constructor which accepts an image.
You can constructor a JLabel with the image and then add the label to the parent.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76

This didn't work, nothing showed up.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068

Looks like the path used for the ImageIcon is not correct- why is a "cd" there in the path?

Mohamed Sanaulla | My Blog
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
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

Joined: Oct 27, 2005
Posts: 19654

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: Adding an Image to a Panel
Similar Threads
Display image
Image Help
Click on different part of Image.
Image problem
Adding an Image to JPanel using NetBeans IDE