Hello. If anybody has a minute, I am kinda wondering about the intrinsic behavior of JButton and JPanel and something I stumbled on by chance.
Say I create two panels in a JFrame, CENTER to display rtf/html content and SOUTH to display buttons triggering various listeners...
When I add Jbuttons ~directly~ to SOUTH, all I see is the last button and it spans the entire width of the JFrame. However, if I create a ~third~ JPanel, in addition to CENTER and SOUTH, and call it BUTTON_BAR, add the buttons to BUTTON_BAR, and add BUTTON_BAR to SOUTH, they look nice. Centered, proportional, etc across the width of the JFrame.
Could anybody explain this intrinsic behavior by JButton and why simply creating a third JPanel makes the buttons behave "regularly"? I can't see it, nothing else is called...all I did was layer JFrame BUTTONS on JFrame SOUTH...more out of modularizing the code than to resolve the button format problem. Thank you very much for reading this and for your time. I kinda hope this was understandable. [ September 21, 2005: Message edited by: Tom Griffith ]
Originally posted by Tom Griffith: When I add Jbuttons ~directly~ to SOUTH, all I see is the last button and it spans the entire width of the JFrame.
Could anybody explain this intrinsic behavior by JButton and why...
This has nothing to do with the behavior by JButton, but the behavior of BorderLayout. In BorderLayout, there can be at most one component in each of the five positions (EAST, WEST, NORTH, SOUTH, CENTER). When you put the second button in the SOUTH slot it removed the first one.
In addition, the SOUTH and NORTH components will be stretched along the X axis (but not the Y axis) to be the width of the container.
Originally posted by Tom Griffith: However, if I create a ~third~ JPanel, in addition to CENTER and SOUTH, and call it BUTTON_BAR, add the buttons to BUTTON_BAR, and add BUTTON_BAR to SOUTH, they look nice. Centered, proportional, etc across the width of the JFrame.
But your BUTTON_BAR is using FlowLayout, which will center its buttons along the X axis and set each to its preferred size. The BUTTON_BAR itself will be stretched along the X axis to be the width of the container, but this doesn't stretch the actual buttons.
Note, however, that if you resize the container to be too narrow for both buttons to fit, that the second one will not be shown. (It will still be a child of the BUTTON_BAR, though, which differs from the situation above.) [ September 21, 2005: Message edited by: Brian Cole ]
If you didn't explicitly set your LayoutManagers and are wondering why they are different even when both are JPanel's the reason is as follows. A JPanel has a default layout of FlowLayout. JFrame, however, specifies that it's default contentPane will have a BorderLayout. Thus if you use new JFrame().getContentPane() and new JPanel() even if they're both JPanels one will have Flow and the other Border.
Hello. Thank you for the info everybody. Yes, I added CENTER and SOUTH with border layout, since I wanted it to fill the pane, and added BUTTON_PANEL to SOUTH sans any kinda layout...but not for simply defaulting BUTTON_PANEL to FlowLayout. Yeah Charu, I've been in and out of the link you posted, but sometimes I get hasty and ahead of myself. Again, thank you everybody.
Screaming fools! It's nothing more than a tiny ad: