SpringLayout - resizing constraints on multiple frames

Hello. I am trying to understand how SpringLayout works. I am trying to paint three JPanels in a JFrame, in such a way that they will all be resized when the JFrame is resized. The following program draws the JPanels in the size and orientation I want, but I can't figure out how to add the right constraints so the EAST side of each JPanel is constrained to the EAST side of the content pane; or how to constrain the width of the JPanels to match the content pane's width. Any help will be greatly appreciated.

package test;

import java.awt.*;

import javax.swing.*;

importstatic javax.swing.SpringLayout.*;

publicclass Test{

publicstaticvoid main(String[] argv){

JFrame frame =new JFrame();

Container contentPane = frame.getContentPane();

JPanel panel1 =new JPanel();

JPanel panel2 =new JPanel();

JPanel panel3 =new JPanel();

panel1.setBackground(Color.red);

panel2.setBackground(Color.white);

panel3.setBackground(Color.blue);

panel1.setPreferredSize(new Dimension(100,100));

panel2.setPreferredSize(new Dimension(100,100));

panel3.setPreferredSize(new Dimension(100,100));

contentPane.setPreferredSize(new Dimension(100,300));

contentPane.add(panel1);

contentPane.add(panel2);

contentPane.add(panel3);

SpringLayout layout =new SpringLayout();

contentPane.setLayout(layout);

layout.putConstraint(NORTH, panel1, 0, NORTH, contentPane);

layout.putConstraint(NORTH, panel2, 0, SOUTH, panel1);

layout.putConstraint(NORTH, panel3, 0, SOUTH, panel2);

layout.putConstraint(WEST, panel1, 0, WEST, contentPane);

layout.putConstraint(WEST, panel2, 0, WEST, contentPane);

layout.putConstraint(WEST, panel3, 0, WEST, contentPane);

frame.pack();

frame.setVisible(true);

}

}

[2645 byte] By [edbeatya] at [2007-10-3 10:12:41]
# 1
I would rather set the layout of JFrame to null and place all panels exactly where i want,and will use the componentResized event to get notified about JFrame resize and resize all the panels accoringly....manually...using setSize method.
AmitavaDeya at 2007-7-15 5:32:55 > top of Java-index,Desktop,Core GUI APIs...
# 2

> I would rather set the layout of JFrame to null and

> place all panels exactly where i want,

> and will use the componentResized event to get

> notified about JFrame resize and resize all the

> panels accoringly....manually...using setSize method.

I think there are very few times that you should ever set the layout to null. I would not recommend it.

zadoka at 2007-7-15 5:32:55 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for the response, but I really am trying to understand how to do this using SpringLayout. When I try to constrain multiple components to the content pane, I think things are getting overconstrained. As a result, the subpanels get stuck to either the east side or the west side, but not BOTH sides of the content pane, which is what I'm looking for.

As an aside, I ended up setting a BoxLayout set to PAGE_AXIS, which worked fine. But two duke dollars to anyone who can explain how to do it using a SpringLayout!

edbeatya at 2007-7-15 5:32:55 > top of Java-index,Desktop,Core GUI APIs...
# 4

> explain how to do it using a

> SpringLayout!

I think your constraints need to look like this:

layout.putConstraint(NORTH, panel1, 0, NORTH, contentPane);

layout.putConstraint(NORTH, panel2, 0, SOUTH, panel1);

layout.putConstraint(NORTH, panel3, 0, SOUTH, panel2);

layout.putConstraint(SOUTH, contentPane, 0, SOUTH, panel3);

layout.putConstraint(WEST, panel1, 0, WEST, contentPane);

layout.putConstraint(WEST, panel2, 0, WEST, contentPane);

layout.putConstraint(WEST, panel3, 0, WEST, contentPane);

layout.putConstraint(EAST, contentPane, 0, EAST, panel1);

layout.putConstraint(EAST, contentPane, 0, EAST, panel2);

layout.putConstraint(EAST, contentPane, 0, EAST, panel3);

And you don't need to set the preferred size of the panels, since you are setting if for the content pane.

zadoka at 2007-7-15 5:32:55 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thanks for the reply! Good call on the preferredWidth. However, the constraints you described don't work as expected. Now, when I resize the JFrame, it looks like:

| R ||

|| JFrame|

| W | bkground |

||__|

| Blue |

||

instead of the desired:

| Red|

||

| White |

||

| Blue |

||

So it looks like the contentPane can only have a single East constraint, the other two are either overwritten, or ignored because the component is overconstrained. Any ideas?

edbeatya at 2007-7-15 5:32:55 > top of Java-index,Desktop,Core GUI APIs...
# 6
I should clarify; it doesn't work properly on Java 1.5.0_08; from the java.net forums, a similar solution works on java 6. Does anyone know of a workaround for java 5?
edbeatya at 2007-7-15 5:32:55 > top of Java-index,Desktop,Core GUI APIs...
# 7

Well, this is what finally worked:

layout.putConstraint(EAST, panel1, 0, EAST, contentPane);

layout.putConstraint(EAST, panel2, 0, EAST, contentPane);

layout.putConstraint(EAST, panel3, 0, EAST, contentPane);

layout.putConstraint(NORTH, panel1, 0, NORTH, contentPane);

layout.putConstraint(NORTH, panel2, 0, SOUTH, panel1);

layout.putConstraint(NORTH, panel3, 0, SOUTH, panel2);

layout.putConstraint(WEST, panel1, 0, WEST, contentPane);

layout.putConstraint(WEST, panel2, 0, WEST, contentPane);

layout.putConstraint(WEST, panel3, 0, WEST, contentPane);

Whereas this doesn't work in 1.5.0_08:

layout.putConstraint(NORTH, panel1, 0, NORTH, contentPane);

layout.putConstraint(NORTH, panel2, 0, SOUTH, panel1);

layout.putConstraint(NORTH, panel3, 0, SOUTH, panel2);

layout.putConstraint(WEST, panel1, 0, WEST, contentPane);

layout.putConstraint(WEST, panel2, 0, WEST, contentPane);

layout.putConstraint(WEST, panel3, 0, WEST, contentPane);

layout.putConstraint(EAST, panel1, 0, EAST, contentPane);

layout.putConstraint(EAST, panel2, 0, EAST, contentPane);

layout.putConstraint(EAST, panel3, 0, EAST, contentPane);

One star to zadok for coming closest to a working solution; the other one goes to whoever can explain the rule for setting the constraint order so it works right off the bat, without having to "jiggle the handle".

edbeatya at 2007-7-15 5:32:55 > top of Java-index,Desktop,Core GUI APIs...