Tue Oct 21, 2008 8:19 pm
I have a group of JCheckBoxes, which I add to a JPanel
checkBoxPanel, which is itself added to a JPanel panel. The
LayoutManager of checkBoxPanel is set to FlowLayout, with the
expectation that the check boxes are arranged in lines, starting a new
line whenever a line does not have enough space left for the next
check box. This works well if the parent of checkBox Panel (which is
panel) itself is FlowLayout. But if panel is set to GridBagLayout, all
check boxes are placed in one line, even if there is not enough space,
pushing some check boxes partially or completely out of the drawn
area. I don't understand how the check box panel's layout manager's
decision whether or not to start a new line depends on the layout
manager of its parent container. Any advice? Thanks!
Tue Oct 21, 2008 8:20 pm
Whenever you nest containers and choose a layout manager that allows it
to think for itself, you open yourself up to this behavior. If a child
container does not have a fixed size, then it can change size to fit its
components. The parent container may accommodate this change, or it may
not -- which may force the child container to rearrange its components.
The interaction here is not simple nor is there a definitive rule for
how it works. It is what it is. If precise positioning is crucial, use a
GridBagLayout and make sure you place lots of constraints so it does not
try to think for itself.
Computers thinking is dangerous: that is what gave us the apocalypse. It
is best if we think for them.
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.