Control the size of the 'components' of a BorderLayout

I would like to control the size of the 'components' of a BorderLayout. I would like to make the North, South, East, and West components smaller, (or make the Center component larger).
How can this be done?
I do not want to increase the size of the dialog box

Below is a class defining the layouts.
To instantiate it, use this:
  ExampleBox eb = new ExampleBox (this);
  eb.show();

***********************************************************

import java.awt.*;

public class ExampleBox extends Dialog
{
  public ExampleBox (Frame parent) // constructor
  {
    super (parent, "Example Box", true);

    Panel wholePage = new Panel();              // create panel
    wholePage.setLayout(new BorderLayout());

    Panel centerPnl    = new Panel();                 // create panel
    centerPnl.setLayout(new GridLayout(2,1,10,10));   // set the layout for this panel

    Panel buttonPnl    = new Panel();           // create panel
    buttonPnl.setLayout(new GridLayout(1,3));   // r,c set the layout for this panel

    Panel labelPnl     = new Panel();           // create panel
    labelPnl.setLayout(new GridLayout(2,1));    // r,c set the layout for this panel

    Label label1 = new Label ("Merry Christmas", Label.CENTER);
    Label label2 = new Label ("and Happy New Year", Label.CENTER);
    Button OkBtn = new Button ("OK");

    buttonPnl.add (new Label());  // fill in blank
    buttonPnl.add (OkBtn);        // add button to panel
    buttonPnl.add (new Label());  // fill in blank

    labelPnl.add (label1);
    labelPnl.add (label2);

    centerPnl.add (labelPnl);
    centerPnl.add (buttonPnl);

    wholePage.add("North", new Label()); // fill in blank
    wholePage.add("South", new Label()); // fill in blank
    wholePage.add("East", new Label());  // fill in blank
    wholePage.add("West", new Label());  // fill in blank
    wholePage.add("Center", centerPnl);  // place center panel
    add (wholePage); // add panel to frame

    setLocation (100, 100);
    setSize (250,150);

  } // end constructor

  // action handler used to close window
  public boolean action (Event evt, Object arg)
  {
    if ("OK".equals(arg))
    {
      dispose();
      return true;
    } // end if

    return false;

  } // end action

} // end class ExampleBox

jdauesAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nbellConnect With a Mentor Commented:
I am not sure why you have so many empty labels. If you are using them for spacing or something there are much better ways of accomplishing that.  If you want to really customize your layout use a GridBagLayout.

As far as a BorderLayout goes consider the following code:

Panel wholepage=new Panel();
Panel n=new Panel();
Panel s=new Panel();
Panel e=new Panel();
Panel w=new Panel();
Panel c=new Panel();

n.setsize(1000,50);
s.setsize(1000,50);
e.setsize(50,900);
w.setsize(50,900);

wholepage.add("North",n);
wholepage.add("South",s);
wholepage.add("East",e);
wholepage.add("West",w);
wholepage.add("Center",c);


In this case you explicity set the sizes of the bordering panes and the center pane gets the "leftovers".
0
 
jdauesAuthor Commented:
How is it that you can set the size of the panels to larger than the frame?

the size of the frame is set to:
setSize (250,150);

yet the size of the panels you have set are larger... 900, 1000


0
 
nbellCommented:
I am sorry, I forgot to add the line:
setSize(1000,1000);

at the end of my code.

I meant only to give you an example of the logic involved. You can apply the same logic to a frame which is (250,150) like this:

n.setsize(250,25);
s.setsize(250,25);
e.setsize(25,100);
w.setsize(25,100);
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jdauesAuthor Commented:
oh, ok.

btw, GridBagLayout is a headache. i used it once and got want i wanted, but it was too much to deal with.

suppose i had just a simple GridLayout of 1 row 4 columns, with a panel in each of them.
by default, each space in that GridLayout is the same size.
is there no way to change the size (without going to GridBagLayout)?
if i draw a parallel from the Borderlayout example you just gave,
theoretically i could do something like:
panel1.setSize (400,100);
panel2.setSize (100,100);
panel3.setSize (400,100);
panel4.setSize (100,100);
(assuming total width of layout is 1000, total height is 100)

i've tried this but it just ignores these statements.


0
 
nbellCommented:
Well, Gridlayout is not supposed to take into consideratioin the preferred size of the components it contains.  The Conatainer which impliments the GridLayout is queried by the Gridlayout as to its size. Gridlayout then gives each row/column a percentage of this size based on the number of rows and columns.

So if you had:

Panel p1=new Panel(),p2=new Panel(),p3=new Panel();
p1.setSize(100,100);
p1.setLayout(new GridLayout(4,4));
p1.add(p2);
p1.add(p3);


In this example p2 and p3 should show up as 25 x 25. Of course this only means that is how much of those panels will be displayed, excess size is not displayed unless you use a ScrollPane or something.


As far as customizing your "field" widths this can be done through constraints objects. LayoutManager2 interface supports this and is the layout manager upon which GridBagLayout is based. GridLayout is based on LayoutManager and does not support the use of constraints.

I don't think that a GridBagLayout is all that tough especially if you write yourself a small convenience function. Consider the following example, which uses a GridBagLayout and a convenience function called constrain to make its use easier.

http://www.oreilly.com/catalog/javanut/examples/section5/AllComponents.java


0
 
jdauesAuthor Commented:
i'll check it out,
thanks for the tips.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.