Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem regarding Border Layout

 
priya gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I typed the following code:-
import java.awt.*;
class L
{
Frame f;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
L()
{
Frame f=new Frame("my frame");
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
f.setSize(1000,1000);
b1.setSize(10,10);
b2.setSize(10,10);
b3.setSize(10,10);
b4.setSize(10,10);
b5.setSize(10,10);
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);
f.setVisible(true);
}
public static void main(String s[])
{
new L();
}
}



and I got 5 buttons which were covering the whole frame..why are not they taking the size which i specified(10,10)..??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BorderLayout ignores one or both dimensions of the preferred sizes of the components it manages; that's just how it works. It tries to respect the vertical size of the NORTH and SOUTH components, and the horizontal size of the WEST and EAST ones, and totally ignores what size the CENTER one wants to be.
 
priya gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that I can not specify the size of any component which I want to add on frame if I m using Border layout..???
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by priya gupta:
It means that I can not specify the size of any component which I want to add on frame if I m using Border layout..???


Yes, that's right. The entire point of BorderLayout is that it resizes all the components to fit into a specific arrangement. If you want something different, then you would use a different LayoutManager.

I'm going to move this to our Swing/AWT forum.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by priya gupta:
It means that I can not specify the size of any component which I want to add on frame if I m using Border layout..???


That's essentially correct. The normal way to put a button in NORTH is to use an itermediate panel, like this:

Panel p = new Panel(); // FlowLayout
p.add(b1);
f.add(p, BorderLayout.NORTH);

That way p will be stretched to be as wide as the Frame, but p will lay out b1 with its preferred size. (Btw, I have found that some people are reluctant to use intermediate panels like this for some reason. I'm not sure why, because they were designed for this kind of use.)

While I'm here, let me point out that b1.setSize(10, 10) has no effect. That's because the layout manager will reset the size of b1 afterward. You would do better with something like b1.setPreferredSize(new Dimension(20, 20)).
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you set the preferred size then the only time a layout manager changes it (at least with my code so far) is when the user resizes the frame to small for all the components to fit with that size and similar problems. Another way would be to set the maximum size to 10 and then it could not get bigger. Or you could set your frames resizable property to false and then you could make it whatever size you want as long as the frame is big enough.
[ April 05, 2008: Message edited by: colton peterson ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by colton peterson:
If you set the preferred size then the only time a layout manager changes it (at least with my code so far) is when the user resizes the frame to small for all the components to fit with that size and similar problems.


A well behaving layout manager never ever *change* the *preferred* size. It might *ignore* it, though. And as explained above, BorderLayout will ignore it in many cases - it's very well documented in the Javadoc, by the way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic