I wanted to know how to make the JFrame's contentPane transparent but the JFrame visible. I saw many pages but all showed on how to make the JFrame border visible but the contentPane visible.
I tried using setOpacity() but that requires the JFrame to be undecorated.
Are there any methods that I can achieve this?
Swing was not designed to paint with transparent backgrounds. You cannot create a transparent contentPane. However, you can fake a transparent background.
Here's a screenshot showing a fake transparent background. I cropped and reduced the image by 50% to get it to display in this answer.
So, how did I do this?
I took a snapshot of the background and painted the background on the
JPanel.This only works if you do not change the background while you're displaying your
JFrame. If you change the background by opening or closing any other applications, this fake will not work.You can move the
JFramearound. Do not move the JFrame close to the edge of the screen, or you'll ruin the illusion.Iconifying and deiconifying the
JFrameworks, but the illusion becomes obvious.You can resize, maximize, and restore the
JFrame.It requires a lot of code to make this fake work properly.
Here's the complete runnable code. I made all the additional classes inner classes so I could post this code as one block.