Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

urgent help with adding an image to jframe

Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I`m trying to add a background to a JFrame as a wallpaper the problem im having is that
the image appears in the centre of the frame and I cant seem to figure out how to make it
cover(the image) the entire frame and for the image to be able to resize if i were to
maximize or minimize the frame
background = new JLabel(new ImageIcon("c:\\windows\\desktop\\bg0063.jpg"));
where do I go from here
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't used SWING, but it looks like the problems you get are because of how you draw the image - it's part of a JLabel, which gets put in the centre of the frame.
You could try setting the frame's layout to a FlowLayout, so it gets put at the start, with something like

NOTE: I'm guessing that's how you use getContentPane(), but I've not used it, so somebody pls correct me if I'm wrong.
You might be better off drawing the image yourself in the JFrame's paint().
It would go something like this:

I haven't tested that code, but I think it's right.
Then you can go into scaling the image according to the size of the frame, etc.
One thing you might get with that is the frame title & sides cover some of the Image - if this happens, use

in your paint().
Hope that helps.
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well have u tried to place the JLabel with the start position and the width and height with setBounds(int x, int y, int width, int height); .. well I guess that should help.
ne way lemme know if that works.
If it doesnt I shall try n find a work around for u r problem..
All the best
Gautham Kasinath
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a code snippet at
which shows how to add a background image to a JTree. It tiles it nicely like you want. I wonder if there is something in that code you could use to do the same for your JFrame.
Hope this helps!
and POOF! You're gone! But look, this tiny ad is still here:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic