I've trying to get two JPanel
s to layer on top of one another, but at the same time, expand to fit the size of their parent. For example, if the JFrame
that contains the two layered JPanel
s is expanded, the layered JPanel
s also expand to fill the entire JFrame
, and vice-versa when the JFrame
is collapsed.
The idea is to create a top layer that contains buttons and other Swing components that control a bottom layer, but the bottom layer can still be seen through the top layer (by setting the top layer's properties to setOpaque(false)
). At the same time, both the top and the bottom layers should fill the available size of their container (the JPanel
or JFrame
that contains the layers).
I have already tried working with JLayeredPane
, but the size of the pane must be known in advanced (as far I have been able to get it working so far). These size constraints will change as the size of the containing JPanel
or JFrame
change.
Thank you in advanced for your help.
Try using a OverlayLayout
, but I'm not sure it will meet your requirements.
Set use a GridBagLayout
on the JLayeredPane
, placing each component at the same gridx
/gridy
and then filling with GridBagConstraints.BOTH
and a weightx
/weighty
of 1
Use the frame's "glass pane" as the top layer. Which will allow you to place controls over the top the frame's "content pane"
See How to use root panes for more details.