Can JPanels background be set to transparent?
My frame is has two JPanels:
- Image Panel and
- Feature Panel.
Feature Panel is overlapping Image Panel.
The Image Panel is working as a background and it loads image from a remote URL.
On Feature Panel I want to draw shapes. Now Image Panel cannot be seen due to Feature Panel’s background color.
I need to make Feature Panel background transparent while still drawing its shapes and I want Image Panel to be visible (since it is doing tiling and cache function of images).
I’m using two JPanel‘s, because I need to seperate the image and shape drawing .
Is there a way the overlapping Jpanel have a transparent background?
Alternatively, consider The Glass Pane, discussed in the article How to Use Root Panes. You could draw your “Feature” content in the glass pane’s
paintComponent()method.Addendum: Working with the GlassPaneDemo, I added an image:
and altered the glass pane’s
paintComponent()method:As noted here, Swing components must honor the opaque property; in this variation, the
ImageIconcompletely fills theBorderLayout.CENTERof the frame’s default layout.