I know this might seem like a duplicate, but I’m not getting anywhere with by experimenting on invalidate/validate/revalidate/repaint, so please bear with me. My panel structure looks something like this:
JPanel/GridBagLayout (1)
+-- JPanel/BorderLayout
+-- JPanel/GridBagLayout (2)
| +-- JTextField (3)
| +-- JComboBox
+-- JPanel/WhateverLayout
...
… and so forth. In my sub panel (2) I change the insets (right and bottom), and I want to layout the whole top panel (1). Is there some easy way to layout everything (preferably from top panel (1) and down, but anything that works is okey). Right now I’ve tried combinations of invalidate/validate/revalidate/repaint on all levels, but nothing seems to work (in fact nothing changes at all). Thanks!
Edit: I found out that GridBagLayout clones the GridBagConstraints as components are added, so the reason my code didn’t work by running revalidate and friends was that I updated the wrong constraints. I found and added a solution to this problem below.
GridBagLayoutclones theGridBagConstraintsas components are added (that’s why changing my original once had no effect on the layout), so I extendedGridBagLayoutto be able to update the actual constraints runtime. The code below sets the layout margins depending on type of component, and if “expanded”, which is what I use to toggle between two modes:Then all I had to do was to call
panel.revalidate()on(1)and layouting works as expected.