Java – gridbaglayout: how to prevent invisible components from crashing
When making the components of the layout managed by gridbaglayout invisible, the arrangement of other components in the layout will change continuously Gridbaglayout appears to "collapse" cells with invisible components, causing other components to change position It's a very annoying behavior, and I've been looking for a way to stop it
One method I have tried is to add a spacer in the same cell. Its fixed size is equal to the preferred size of components that may be hidden. However, if you want to change the preferred size of components, you will encounter a lot of trouble to keep them synchronized Is there a better way?
In fact, if gridbaglayout really completely ignores cells with invisible components, it won't be so bad, but it still considers cell illustrations, so if you have a layout of input fields and use illustrations to neatly arrange the gap between them, then hide the middle component and remove it from the layout but leave a gap, This usually means that the surrounding components flow together, but there is an ugly double wide gap between them
Solution
How about using JPanel (or @ r_649_2419 @) as a placeholder? Put JPanel in each cell and add components to the panel If you can't see the component, the panel should still be there