Forums Register Login

SpringLayout - handcoding

+Pie Number of slices to send: Send
Hi,
Can't get SpringLayout to do what I want, and I'm not asking for much. HEEELLLPP, pleeeeeease!!!
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
What about GridBagLayout?
That would allow you to set insets, component weight, etc...Seems to me to be the best LayoutManager.
:roll:
+Pie Number of slices to send: Send
> What about GridBagLayout?

Thanks for the suggestion, but it scares me. I'm sure(?) it can do what I want, but now I'm using RelativeLayout I don't think I could bear to use anything else. Putting the layout in an XML file is so nice, and it's so easy. Sorry, I sound like an advert...
Cheers
Simon
+Pie Number of slices to send: Send
 

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.


The putConstraint method sets the first edge to be x pixels from the other specified edge, which should be something that can be calculated. In other words it creates a strut, a Spring that has equal pref/min/max values, between the first edge and the second edge. That's why the first two set the edges of the JList based upon what the edges of the container are plus the strut. The second set the edges of the container based upon the edges of the JList plus the strut.
Your JList size is being determined by a spring and a strut. The width is a strut because its min/max/pref sizes are all set to 100 and consequently the width should not change. Horizontally you have a strut of 5, then the width of the JList which is a strut, and another strut of 5. If the frame is widened then SOMETHING has to give, it would be impossible to maintain all three of these. On my machine the strut connecting the east of the container to the east of JList gives way and the JList is not widened leaving empty space. If on your machine the JList was widening then that was what was giving way in order to maintain the other two struts. The height is a spring because it has varying min/pref/max sizes (10, 100, 32767) and can change, so it will absorb any changes in height.

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 the biggest problem I've had with Spring. I wish I knew how to effectively work around that to have a situation where everything scales properly. I don't and I haven't found any examples either.
While you seem to have moved on hopefully this will help, or maybe someone else with similar problems will see this. I had the same misconception about the putConstraint method that caused endless trouble until I realized how it was calculating the edges.
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1729 times.
Similar Threads
JLabel and JTextArea do not show properly in ubuntu
passing value from jcombobox to other classes?
My .PNG background will not export ||THIS HAS BEEN RESOLVED!!! THANKS!!||
Problem With SpringLayout Component Placement
SpringLayout applied to a JPanel
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 02:49:37.