NiftyGui Setting Position in Java

How does one set the position of an Element in Java (assuming the parent Element is using an absolute layout)?

For example, I would like to do something like:
[java]Element e = screen.findElementByName(“blah”);
e.setX(100 * a + b - c);[/java]

But currently I’m forced to do:
[xml]
<panel name=“blah” x=“35px”/>
[/xml]

This is insufficient since I don’t know what the value for x will be at startup, but I’d like to change it in the onStartScreen() function.

This is a copy/paste, but it should be enough to push you in the right direction.

[java]
private void updateShieldImage(final float shieldLeft) {
Element oldImage = screen.findElementByName(“healthBar”);
String size = getShieldImgHeight(shieldLeft);
oldImage.setConstraintHeight(new SizeValue(size));
oldImage.setConstraintY(new SizeValue(getShieldImgPosY(shieldLeft)));
oldImage.getParent().layoutElements();
}
[/java]

1 Like

I had tried the constraints before, but they weren’t working. I must have had something missing or screwed up because it’s working now.

Thanks!!

You need to call layoutElements() to kick Nifty into updating the new stuff.