How do you add Background Image in JPanel (NetBeans GUI Builder)

Sanshayan picture Sanshayan · Jan 2, 2014 · Viewed 59.4k times · Source

I have JTabbedPane with five tabs and each have Jpanel i want add different images for each panel in NetBeans IDE

Answer

Paul Samsotha picture Paul Samsotha · Jan 4, 2014
  1. Right click on your project and add a new package, name it resources. This will need to be done so Netbeans imports your picture into that folder
  2. Add a JLabel to the Panel
  3. Hightlight the JLabel and go to the Properties pane on the right
  4. In the property that says icon click the ... button. That will take you to a dialog
  5. Choose External Image radio button
  6. Click the ... next to the file text field
  7. Pick your Image and click OK.
  8. Click Import to Project
  9. Click OK, You should see the image in your graphic layout

Note this will only give you an Image, but doesn't really act as a background, because the JLabel is it's own component. I'm not really sure how to achieve a backgroud with GUI Builder. I'm not too familiar with the technology. Though if you were to write your own code, there are numerous answers here on SO that I'm sure you'll find useful. The only tricky thing about GUI Builder is that they have auto-generated code that you really can't play around with, which circumvents what I know about creating a background image.

enter image description here

enter image description here

enter image description here


NOTE : this only works for JLabels as JPanels don't use Icon. An alternative would be to hand write your own JPanel code in the constructor and draw the image, overriding the paintComponent method.