Solved

Control the size of the 'components' of a BorderLayout

Posted on 1998-12-24
6
554 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:jdaues
  • 3
  • 3
6 Comments
 

Accepted Solution

by:
nbell earned 50 total points
ID: 1230118
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
 

Author Comment

by:jdaues
ID: 1230119
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
 

Expert Comment

by:nbell
ID: 1230120
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

Author Comment

by:jdaues
ID: 1230121
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
 

Expert Comment

by:nbell
ID: 1230122
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
 

Author Comment

by:jdaues
ID: 1230123
i'll check it out,
thanks for the tips.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question