3 Replies - 5010 Views - Last Post: 07 December 2009 - 04:59 PM Rate Topic: -----

#1 edmit15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 09

Swing Background Image with components

Posted 07 December 2009 - 12:12 PM

Hello,

I am trying to add a background image to a JPanel within a JFrame.
The supplied code below works fine to display the image.
However, when I add another component (JButton, or an additional JPanel) to the panel with the background it does not show through the background image(sometimes a JButton does but only when moused over). My question is, how do I add a background image that I can then add other components on top of?


 
@Override
  protected void paintComponent(Graphics g)
  {
	super.paintComponent(g); 
	if (image != null)
	  g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
  }



Is This A Good Question/Topic? 0
  • +

Replies To: Swing Background Image with components

#2 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Swing Background Image with components

Posted 07 December 2009 - 02:09 PM

View Postedmit15, on 7 Dec, 2009 - 11:12 AM, said:

Hello,

I am trying to add a background image to a JPanel within a JFrame.
The supplied code below works fine to display the image.
However, when I add another component (JButton, or an additional JPanel) to the panel with the background it does not show through the background image(sometimes a JButton does but only when moused over). My question is, how do I add a background image that I can then add other components on top of?


 
@Override
  protected void paintComponent(Graphics g)
  {
	super.paintComponent(g); 
	if (image != null)
	  g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
  }




The Override Paint Component will indeed dissapear when adding other components to the form (Im not claiming to know exactly why, but it does). Here is how i would approach the situation:

1. Add this to your panel:
JPanel p = new JPanel();// an example of your panel.
p.setLayout(null);



2. Then make a JLabel as big as your panel size:

JLabel l = new JLabel("");
l.setBounds(x,y,width,height) // you need to set bounds for null layout panels!!!



3. Then just add all the other components to it! When you set the layout to null, it adds the components in order.
So! bascially your background is added to your form first. Like:

JFrame f = new JFrame("");

JPanel p = new JPanel();
p.setLayout(null);
//p.setSize(500,500); // make the size of the Frame basically

f.add(p); // add the panel to your frame

JLabel background = new JLabel("");
background.setIcon(new javax.swing.ImageIcon("Image Location"));
background.setBounds(0,0,500,500); // for example of course
p.add(background);




That is what i have done in the past to make a background layer, however it may not be the best way xD but it does truely work!
Was This Post Helpful? 1
  • +
  • -

#3 edmit15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 09

Re: Swing Background Image with components

Posted 07 December 2009 - 02:37 PM

Thank you so much, this example was just the type of solution I was looking for. This was my first post here ever and I'm glad it helped me out. You have no idea how many hours I spent searching google for a good solution.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,171
  • Joined: 06-March 08

Re: Swing Background Image with components

Posted 07 December 2009 - 04:59 PM

This draws your background (in a big JLabel) but does not allow you to have other JComponent (JButton, JLabel, JCheckBox, ....)
in the same panel as the question asked.

The problem is that when you do
public void paint(Graphics g) {
	super.paint(g);	// this draw the background and the JComponent
	.... your other drawing here if it fill the panel it destroys what was done previous line
}



public void paint(Graphics g) {
	... now I can draw my background
				... but I will also have to draw by JComponent by hand :-(
}



Never tried it but the only way I can see that done is with a LayeredPane

This post has been edited by pbl: 07 December 2009 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1