• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add backgroung image to JPanel?

 
Aman Verma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Would anyone please explain me how to set an image as background to JPanel?

Thanks in advance.
-Aman
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Background suggests that you may be doing things in the foreground like adding components. If so you could render the image in the 'paintComponent' method of the JPanel. If there are no components added to the panel you'll need to provide size information for it by calling 'setPreferredSize' or overriding 'getPreferredSize'. Use the image dimensions for the size if you want the panel to be the size of the image.

If you only want to show an image as–is, JLabel makes this easy with ImageIcon to hold the image. The swing tutorial has a page on using icons.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic