I'm working on a project which adds (sub) JPanel
objects to another panel with GridBagLayout
. These sub-panels are removable, leaving a blank spot in the layout, like so:
When I add another panel into this, I would like it to first fill any empty spaces that exist, then continue filling sequentially like it does already.
I can figure out how to do this with one empty space, just not quite with many of them.
Here's what I can best figure: have an ArrayList
to hold the x-y co-ords from removed objects (can you get that? get the GridBagConstraints
from a component?)
Before remove the sub panel you can get the constraints using GridBagLayout's method
public GridBagConstraints getConstraints(Component comp)
and store them in a list (let's name it freeConstraintsList).
On add get free Constraints from the list and use to add the new sub panel (of couse removing the used constraints from the freeConstraintsList).
If the list is empty introduce the new Constraints.