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:
<panel name=“blah” x=“35px”/>

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.

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)));

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.


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