Hi,
The following code extract successfully creates a list box anchored to
the top-left, and that expands vertically, but has a fixed width.
public class EPoS3 extends JFrame {
public EPoS3() {
super("EPoS");
this.setSize(600, 400);
this.setLocation(50, 50);
Container content = this.getContentPane();
SpringLayout sl = new SpringLayout();
content.setLayout(sl);
//--"Messages" list box--
String items [] = { "Alpha", "Beta", "Gamma" };
JList lstMessages = new JList(items);
//--Current Message panel--
JPanel pnlMessage = new JPanel();
pnlMessage.setBackground(Color.blue);
//--Spring layout--
content.add(lstMessages);
sl.getConstraints(lstMessages).setHeight(Spring.constant(10, 100,
32767));
sl.getConstraints(lstMessages).setWidth(Spring.constant(100));
sl.putConstraint(SpringLayout.WEST, lstMessages,
5,
SpringLayout.WEST, content);
sl.putConstraint(SpringLayout.NORTH, lstMessages,
5,
SpringLayout.NORTH, content);
sl.putConstraint(SpringLayout.EAST, content,
5,
SpringLayout.EAST, lstMessages);
sl.putConstraint(SpringLayout.SOUTH, content,
5,
SpringLayout.SOUTH, lstMessages);
}
}
Q1: For the first two constraints the anchor is the panel and the
dependant is the listbox. However for the last two, this is inverted.
I can't quite figure out why, this may explain why I'm having other
issues.
If the following is now added to the bottom...
//--panel--
content.add(pnlMessage);
sl.getConstraints(pnlMessage).setHeight(Spring.constant(10, 100,
32767));
sl.getConstraints(pnlMessage).setWidth(Spring.constant(10, 10,
32767));
sl.putConstraint(SpringLayout.WEST, pnlMessage,
5,
SpringLayout.EAST, lstMessages);
sl.putConstraint(SpringLayout.NORTH, pnlMessage,
5,
SpringLayout.NORTH, content);
sl.putConstraint(SpringLayout.EAST, content,
5,
SpringLayout.EAST, pnlMessage);
sl.putConstraint(SpringLayout.SOUTH, content,
5,
SpringLayout.SOUTH, pnlMessage);
Q2: This no longer works. The list box is suddenly taking on the
preferred height instead of expanding with a 5 pixel margin. If I set
it to a very large value, the margin is not created. I've tried
switching the south constraint anchor/dependency around, and both
combinations using the panel and listbox, i.e. linking the south of
the panel to the south of the listbox.
I've looked at the SUN + O'Reilly tutorials on SpringLayout, but still
can't get it. All Help gratefully received, as this is driving me
nuts.
I'm using 1.4.2_02 on Win XP.
Cheers
Simon
Simon - 16 Dec 2003 09:50 GMT
I've think I've figured it out, but have decided to abandon
SpringLayout, and use RelativeLayout (see O'Reilly web site). So for
anyone who was curious...
> Q1: For the first two constraints the anchor is the panel and the
> dependant is the listbox. However for the last two, this is inverted.
> I can't quite figure out why, this may explain why I'm having other
> issues.
I'm guessing this is because I'm using positive values, I think the
anchor value needs to be negative for the last two items, due to the
direction.
> Q2: This no longer works. The list box is suddenly taking on the
> preferred height instead of expanding with a 5 pixel margin. If I set
> it to a very large value, the margin is not created. I've tried
> switching the south constraint anchor/dependency around, and both
> combinations using the panel and listbox, i.e. linking the south of
> the panel to the south of the listbox.
This is because an edge can only support one Spring. When I use the
second object, the first Spring for the relevant Container edge is
automatically dropped.