This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Problem regarding Border Layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem regarding Border Layout" Watch "Problem regarding Border Layout" New topic
Author

Problem regarding Border Layout

priya gupta
Greenhorn

Joined: Apr 04, 2008
Posts: 4
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
priya gupta
Greenhorn

Joined: Apr 04, 2008
Posts: 4
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Sep 20, 2005
Posts: 862
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)).


bitguru blog
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

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 ]

www.mormon.org
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
Consider Paul's rocket mass heater.
 
subject: Problem regarding Border Layout
 
Similar Threads
Frames
BorderLayout- Help required
Java awt & Stringbuffer
bitwise operator precedence
LAYOUTS. PLS HLP !!!