File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Make it so: Java DB Connections & Transactions this week in the JDBC 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: 10769

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: 3152

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: 20197

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:
subject: Adding an Image to a Panel
It's not a secret anymore!