• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel and buttons

 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the behaviour you desribe occurs when you add a button directly to the frame,
instead of adding it/them to the panel, then adding the panel to the frame.

if this is not the case, post a sample program - a frame with a panel in center
and another panel (with 2 buttons) in south
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charu Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, it's just that JFrame by default uses BorderLayout and JPanel uses Flow Layout..

Follow this link to get a good understanding of LayoutManagers.These are the basics that you'll need to know for developing any gui using Swing..
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

All the Best!!
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!