Thao Nguyen
Thao Nguyen

Reputation: 901

Borderlayout gui adding to borders

I'm trying to add 3 panels to the border layout only North South and West

something like

[A]

     ___  ______________
    |   ||    P2        |
    |   ||______________|
    |P1 | ______________
    |   ||    P3        |
    |___||______________|

I try to do something like

JFrame window = new JFrame();
window.setLayout(new BorderLayout());
window.add(P1, BorderLayout.WEST);
window.add(P2, BorderLayout.NORTH);
window.add(P3, BorderLayout.SOUTH);

It ends up like

[B]
         ______________
        |    P2        |
        |______________|
         ___
        |P1 |
        |___|
         ______________
        |    P3        |
        |______________|

Do I have to add like a gap as the Center to avoid the issue? I tried just putting P2 and P3 into another Big panel and adding, P1 - West and Big Panel- Center is there any other way around this? Or should I just try a different Layout.

Upvotes: 3

Views: 702

Answers (3)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

That's just the way that BorderLayout works; the NORTH and SOUTH components extend horizontally over and under the WEST, CENTER, and EAST components.

Your system with two BorderLayouts is perfectly fine. You could achieve the same thing with GridBagLayout or MigLayout, but I'll guarantee you it'd take much longer to implement. Using intermediate panels is a valid way to do things.

Upvotes: 3

Emmanuel Bourg
Emmanuel Bourg

Reputation: 10988

Try a MigLayout instead of BorderLayout. The code would look like this:

setLayout(new MigLayout("wrap 2, fill"));
add(P1, "span 1 2, grow");
add(P2, "grow");
add(P3, "grow");

http://www.miglayout.com

Upvotes: 0

Csujo
Csujo

Reputation: 507

JFrame window = new JFrame(); window.setLayout(new BorderLayout()); window.add(P1, BorderLayout.SOUTH); window.add(P4, BorderLayout.WEST);

P4.setLayout(new BorderLayout()); P4.add(P2, BorderLayout.NORTH); P4.add(P3, BorderLayout.SOUTH);

Upvotes: 0

Related Questions