0

i want to add an Image as a background to a JFrame but not like

    this.setContentPane(new JLabel(new IamgeIcon("path of image file"));

When i use the above code line i can not see my other components added to the same JFrame. I want to set an image as a background as we see in the html pages where body has a background image and we see other components like buttons and textboxes over it. Can anyone please show me how to do that? And I want to attach 2 more panels to same JFrame.Please attach the image of your code output.

  • looks like a duplicate http://stackoverflow.com/questions/2645452/background-image-in-a-jframe?rq=1 – Will Hartung Jan 17 '14 at 06:55
  • @WillHartung yes... thanx! I did not find that in searches! – Deepsthecoder Jan 17 '14 at 07:00
  • @WillHartung, but if i make such a panel with an image painted over it, and add it to JFrame using border layout,it will take it's place at either north or south or center or east or west. It will not act as a whole background.... isn't it? I have other 2 panels to add to same JFrame... – Deepsthecoder Jan 17 '14 at 07:08
  • A component added to the `CENTER` of a `BorderLayout` will fill the entire space so long as not components are added at the other positions. – Andrew Thompson Jan 17 '14 at 07:41
  • See also [this answer](http://stackoverflow.com/questions/21168119/gradient-background-in-a-jframe/21171391#21171391) for an MCVE much like you are trying to achieve (with nice screen-shots). – Andrew Thompson Jan 17 '14 at 07:43

1 Answers1

1

You can use JLayeredPane

JLayeredPane adds depth to a JFC/Swing container, allowing components to overlap each other when needed.

link how to use Layered Panes

and check this example

Ashish
  • 1,943
  • 2
  • 14
  • 17
  • I think a `JLayeredPane` is not really the way to go here, and would need to see an MCVE before I'd be convinced otherwise. As to the links, they should best have descriptive text, like the 2nd, as opposed to text of `link`.. – Andrew Thompson Jan 17 '14 at 07:52
  • This [`AnimationTest`](http://stackoverflow.com/a/3256941/230513) use the default layout for components; click few times and then resize to see the effect. – trashgod Jan 17 '14 at 13:16