Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Layout with Nested JPanels

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!



Re: Layout with Nested JPanels

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.

Post a reply
  Related Posts  to : Layout with Nested JPanels
 containers use a border Layout as their default layout     -  
 Frame with Null Layout- set layout NULL     -  
 Nested function     -  
 Break in Nested Loops     -  
 nested ordered list     -  
 Nested classes(inner class) in java     -  
 specify a container's layout     -  
 BoxLayout usage example - Box-Layout     -  
 Window-Frame-with Diagonal Layout     -  
 Slice and Export a Website Layout     -