Solved

Control the size of the 'components' of a BorderLayout

Posted on 1998-12-24
6
544 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now