0

I made a image lists like this in java:

static String[] imageList1 = { "images/bgs/bg.png", "images/bgs/image1.png", 
       "images/bgs/image2.png", "images/bgs/image3.png" };

I use the image in the image list as a background like this:

JLabel background = new JLabel(new ImageIcon(imageList1[0]));

The problem is when I run the program the whole frame was blank(supposedly the frame consists from only one picture image and one button) but if I minimize and then maximize or adjust the width or height of the frame, it suddenly shows the image and the button.

mmBs
  • 8,421
  • 6
  • 38
  • 46
  • you can not use Jlabel as a background image.. – Java Man Feb 04 '14 at 11:24
  • 1
    Did you `setVisible()` _before_ or _after_ adding the `JLabel`? Consider posting a simple runnable example for help faster. As of now, we can only make guesses. – Paul Samsotha Feb 04 '14 at 11:42
  • 1) For better help sooner, post a [Minimal Complete Tested and Readable Example](http://stackoverflow.com/help/mcve) (MCTRE). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Feb 05 '14 at 02:12

1 Answers1

1

Have you tried calling revalidate(); repaint(); ?

That might fix your issue, but what you are looking for is the JImagePanel class. It is a third-party util and should make your life much easier. Get the source here.

Then, just add this panel to the root of the JFrame and viola!

dberm22
  • 3,187
  • 1
  • 31
  • 46
  • I recommend calling `repaint()` after setting the `JLabel`'s icon. `revalidate()` only makes sense if you're altering the component hierarchy. – Gorbles Feb 04 '14 at 13:50