The documentation of SpringLayout says that it´s available since java 1.4, I have like 7 old macs that can´t be upgraded to any newer version of mac os 10.5 which leads me to have only java 1.5.0_30 at most. The problem is that I have an app that works with spring layout and I'm always getting this stack back trace:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.SpringLayout$SpringProxy.getValue(SpringLayout.java:674)
at javax.swing.Spring$CompoundSpring.getValue(Spring.java:420)
at javax.swing.Spring$CompoundSpring.getValue(Spring.java:420)
at javax.swing.SpringLayout.layoutContainer(SpringLayout.java:1004)
at java.awt.Container.layout(Container.java:1401)
at java.awt.Container.doLayout(Container.java:1390)
at java.awt.Container.validateTree(Container.java:1473)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validate(Container.java:1448)
at java.awt.Window.show(Window.java:516)
at java.awt.Component.show(Component.java:1317)
at java.awt.Component.setVisible(Component.java:1270)
Is there any solution, any jar or something similar?
Please avoid answers like "Upgrade you computers"
UPDATE: I dicovered that there is SpringLayout on Java, but you cant use VERTICAL_LAYOUT with SOUTH. I don´t know why.
UPDATE 2: Following the instruction of Ian I did the following code and it is still not working on Java 5
springLayout.putConstraint(SpringLayout.NORTH, myImage,
Spring.sum(springLayout.getConstraint(SpringLayout.SOUTH, myPanel) ,
Spring.sum(Spring.scale(Spring.minus(springLayout.getConstraint(SpringLayout.HEIGHT, myImage)),0.5f),
Spring.scale(Spring.minus(springLayout.getConstraint(SpringLayout.HEIGHT, myPanel)),0.5f)
)
), SpringLayout.NORTH, myPanel);
UPDATE 3: Replacing springLayout.getConstraint(SpringLayout.HEIGHT, myImage)
with springLayout.getConstraints(myImage).getHeight()
it worked perfectly fine. :)
The VERTICAL_CENTER
constraint type was introduced by Java 6 which is why it doesn't work for you in Java 5. But the invariants specify that
VERTICAL_CENTER = NORTH + HEIGHT/2
SOUTH = NORTH + HEIGHT
Substituting and re-arranging these equations we get
NORTH = SOUTH - HEIGHT
VERTICAL_CENTER = SOUTH - HEIGHT/2
VERTICAL_CENTER + HEIGHT/2 = SOUTH
HEIGHT/2 = SOUTH - VERTICAL_CENTER
HEIGHT = 2*(SOUTH - VERTICAL_CENTER)
So if you are currently setting SOUTH
and VERTICAL_CENTER
then you can achieve the same effect by setting the height to a spring of size 2*(SOUTH - VERTICAL_CENTER)
. So replace
constr.putConstraint(SpringLayout.SOUTH, southSpring);
constr.putConstraint(SpringLayout.VERTICAL_CENTER, vcSpring);
with
constr.putConstraint(SpringLayout.SOUTH, southSpring);
constr.setHeight(
Spring.scale(Spring.sum(southSpring, Spring.minus(vcSpring)),
2.0f));