Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding background image to JPanel

Status
Not open for further replies.

sarahnice

Technical User
Nov 18, 2002
101
IE
Is there any way to add a background image to a JPanel or JFrame, so that I can add JButtons, JTextfields, etc., on top of the image.

Any help?

Thankis
:)
 
Try subclassing the JPanel and then overriding the paintComponent(Graphics g) method. Make sure you call super.paint() somewhere, or just paint your components (the JPanels and JFrames) yourself by calling their paint(g); method with the graphics you get.

public void paintComponent(Graphics g)
{
g.drawImage(0, 0, this.getSize().width, this.getSize().height, this);
ArrayList list = myOwnComponentArrayListIDefinedAndFilledSomewhereElse;
for (i = 0; i < list.size(); i++)
{
((Component)list.get(i)).paint(g);
}
}

Hope this helps - I think there'll be a way more elegant solution for drawing the subcomponents somewhere out there...
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top