Creating a type of BorderLayout with 2 PAGE_START areas

Is there a better way to recreate this type of layout without having to use this many containers? Also, in my actual program, some empty space is created under the two bordered panels, presumably from all of the extra space from the embedded BorderLayouts (CENTER, PAGE_END, etc.) In my main program the JFrame uses a BorderLayout, and in the CENTER portion I have a CardLayout. One of the cards uses BorderLayout again with a JTabbedPane within its CENTER area. It is within one of these tabs that I wish to use this double PAGE_START type of layout. The CENTER portion of the frame's BorderLayout actually adds a JScrollPane, with the CardLayout inside it. When I use the pack() command the program does bring the frame's bottom up to the bottom of the second bordered pane, but because of this the vertical scrollbar appears, in order for the empty space immediately below to be contained also. I'm sure if there's a better way of doing this I won't have the problem of this extra empty space, yet I couldn't recreate it in this sample code.

Is there a better way to implement 2 panels into some sort of double PAGE_START configuration without this confusing setup? I'd like the panels to retain their heights, and stretch width-wise, both of whom, like in the code below, are put up at the top of their parent container.

Here's the code:

import javax.swing.border.TitledBorder;

import javax.swing.*;

import java.awt.*;

publicclass Test

{

publicstaticvoid main(String[] args)

{

//Initiate everything

JFrame frame =new JFrame();

JPanel main =new JPanel(new BorderLayout());

JPanel pOne =new JPanel(new BorderLayout());

JPanel sub =new JPanel(new BorderLayout());

JPanel pTwo =new JPanel();

TitledBorder bOne = BorderFactory.createTitledBorder("Area One");

TitledBorder bTwo = BorderFactory.createTitledBorder("Area Two");

//Apply borders

pOne.setBorder(bOne);

pTwo.setBorder(bTwo);

//Add components

frame.add(new JScrollPane(main));

main.add(pOne, BorderLayout.PAGE_START);

main.add(sub, BorderLayout.CENTER);

sub.add(pTwo, BorderLayout.PAGE_START);

pOne.add(new JTextField("Component 1"));

pTwo.add(new JButton("Component 2"));

//Finalize and display window

frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

Thanks!

[3553 byte] By [Jason102a] at [2007-11-26 14:22:07]
# 1

GridBagLayout and SpringLayout can both do it. Here's a GBL

example. You can only remove so many containers, because

TitledBorder needs a Container to wrap around.

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

public class Test {

public static void main(String[] args) {

final JPanel titleOne = new JPanel(new BorderLayout());

titleOne.add(new JTextField("Component 1"), BorderLayout.CENTER);

titleOne.setBorder(new TitledBorder("Area One"));

final JPanel titleTwo = new JPanel();

titleTwo.add(new JButton("Component 2"));

titleTwo.setBorder(new TitledBorder("Area Two"));

final JFrame frame = new JFrame();

final Container container = new JPanel(new GridBagLayout());

final GridBagConstraints gbc = new GridBagConstraints();

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.weightx = 1;

gbc.gridx = 0;

gbc.gridy = 0;

container.add(titleOne, gbc);

gbc.gridy++;

container.add(titleTwo, gbc);

gbc.fill = GridBagConstraints.BOTH;

gbc.weighty = 1;

gbc.gridy++;

container.add(new JPanel(), gbc);

frame.getContentPane().add(container);

frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

es5f2000a at 2007-7-8 2:14:04 > top of Java-index,Desktop,Core GUI APIs...
# 2
> ..I'd like the panels to retain their heightsDid you try setPreferredSize on the panels?
Andre_Uhresa at 2007-7-8 2:14:04 > top of Java-index,Desktop,Core GUI APIs...
# 3
Yes that works, thanks. And just out of curiosity how would you use SpringLayout to do it?
Jason102a at 2007-7-8 2:14:04 > top of Java-index,Desktop,Core GUI APIs...
# 4
I wouldn't. I don't use SpringLayout. It hurts my head.
es5f2000a at 2007-7-8 2:14:04 > top of Java-index,Desktop,Core GUI APIs...
# 5
Haha OK well whatever works - at least anything other than my original way. Your example taught me a few more things about how to use GridBagLayout, so thanks for that too. ^_^
Jason102a at 2007-7-8 2:14:04 > top of Java-index,Desktop,Core GUI APIs...