This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Ok, here is an easy one, but appearntly not for me. I have a JFrame with a background Image which is placed on the JFrame by overriding the paint(Graphics g) method because I wanted it as a Backgroud Image, not as a JLabel. So now I add a Button to the ContentPane of my JFrame and it does not show up until I click on the JButton. Here is my code...
Hi Gregg, if you want to have a background image on your frame, then you have to use a panel for it, so your frame has a BorderLayout and only your ImagePanel is added. There is already a great toolkit available, which is able to have background images. It is free and you can download it here. There you have to have a look to the class kiwi.ui.KPanel. Hope this helps Rene
Thanks for the Kiwi Link. It looks like a neat package. However, your first statement is not accurate. You can add a background image to a JFrame without using a JPanel. I did that. Whether or not I can make it work the way I need it to is the problem. I have temporarily used a JLayeredPane to achieve what I need until someone may be able to provide an answer to my original problem (if there is one ).
You can paint directly to the JFrame... but it causes problems because JFrame uses some special kind of painting to paint all it's contents. The best thing to do (in my opinion) would be to replace the content pane of the JFrame with a custom JPanel that paints your image...
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
JFrame is one of the few "top-level" components in Swing... the paint() method calls other methods that control the painting of the content pane and all the components within it. To get that behavior, you have to call super.paint(g) within the overridden paint method. Unfortunately, some of the super.paint() code looks like it clears the screen by painting over it (probably some of the built-in double buffering done by Swing...), so if you paint the image *before* you call super.paint(g) it gets painted over, and so it looks like it never shows up. If you paint the image *after* you call super.paint(g) it paints on top of everything.