Solved

Control the size of the 'components' of a BorderLayout

Posted on 1998-12-24
6
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 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