I am new to Swing and Java GUI, but not Java, however far from an expert. I want to add an image as background in a class that extends a class that extends JFrame. Beforehand this class contains a couple of JLabels (Sliders etc), nothing complex, and is working fine. It will however be enhanced with "dynamic" functonality, i.e., I must prevent that the whole background image is constantly updated. Further, I want the background to be autonomous; fire and forget, like setting the background of an excel spreadsheet, still having full functionality of the spreadsheet.
Using the following code, I am able to display the image properly as a background image:
However, as soon as I start adding the other functionality, i.e. the other JLabels in a GridLayout, then the GUI update goes awry, only updating the part of the background image that is "beneath" the other JLabels. In other words, the code above is interacting with the rest of the code. This is exactly what I would like to avoid. THis happens: The rest of the background image, having a area/size that is larger than the GridLayout, is never shown at all, ie there is a clipping effect.
Q: Is there a simple way to define a background image to a JFrame, via JLAbel or whatever, but in such a way that it will be always automatically updated "according to good coding practice". Something like this: MyJFrame.setBackgroundImage ( image, FREEZE). With FREEZE I mean to indicate that the image is in no need of update by itself.
I see in the tutorials that I may use JLayeredPane, but isn't there a simpler way ?? Many thanks for anything that may help me further on, be it general guidance, pointer to interesting javadocs, principal code snippets, ... I am really a bit stuck after several days of investigations.
I have tried the "Background image on JPanel", but still in a dire straits. In my double nested JFrame class, see initial post, I have this code:
This loads the image, but shrinked to size approx 10x10pixel. The actual size is 1024x309 (for certain). However, the this.getWidth and this.getHeight reports 1018x277
1. Any idea why the correct size is not detected?
2. What did I do wrong or forgot in the above code ?
I also mention that in class BackgroundPanel I use this code:
...as I could not get the original code to work, namely this:
I anticipate that is not important?
Finally, I add that, if I load the background image as an Icon, then reports the correct size 1024x309.
Joined: Jun 09, 2003
> This loads the image, but shrinked to size approx 10x10pixel.
post the program you've used - stripped down to just the frame + how you add BackgroundPanel(),
and the code where you check sizing
> image = Toolkit.getDefaultToolkit().getImage(
won't work if you .jar the app
Joined: May 31, 2009
I gave up today on setting the background. It seems to me it is a bit on the fringe and better stick to a major route: I am using JLayeredPane and this is working fine for my purpose, as I define the lowest layer as background. Do you still want me to post the code? To my understanding, the essential part is already in prev posting.
Regarding the "toolkit..getImage()" I have images launched this way in a Jar working just fine, but I needed to specify absolute windows filepath, else the program could not find the images, even if they were in the Jar. Another mystery...
Thanks for help!
Joined: Jun 09, 2003
> Do you still want me to post the code? To my understanding, the essential part is already in prev posting.
It works exactly the way it should for me, so I'm trying to work out what it is you're doing differently.
'essential parts' don't help, because it generally is the perceived-to-be non essential parts that contain the problem.
that is why it is always best to post a stripped-down demo program that we can copy/paste/compile/run,
and see the problem on the screen.
Joined: May 31, 2009
I see your point indeed. I am sorry, but after I got the alternative to work, I cleaned up the file quite a bit as I have quite a lot on the todo list and must press on. I think I will not be able to reproduce it, but the code above is ALL that has been replaced by JLayeredPane and now works. However, this code (class) is as I said initially an extension of an extension of JFrame, and in order to study the particular case, worst case, you would need to study a lot of code. Next time I shall try to present a more complete example. Thanks again.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: howto: add image as background and then forget about it