aspose file tools*
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding an Image to a Panel" Watch "Adding an Image to a Panel" New topic
Author

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: 10535
    
    9

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: 3099
    
  33

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
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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