[SOLVED] Nifty Gui: How to print a string returned by Java method in XML text/text field/label (on a popup)?

Java

public String getString()
{
    return "Hello";
}

XML

<control name="textfield" id="textfield" text="getString()" />

This is an example of what I’m looking for.

Whatever my getString() method returns should be printed out in the text/text field/label. But I’m not sure how to go about doing that.

I’ve called other methods within XML using

<interact onClick="method()">

and they execute, but that’s for clicking buttons, etc.

I tried coding it from Java using

nifty.getCurrentScreen().findNiftyControl("textfield", TextField.class).setText(getString());

This code works on other text/text fields/labels that are on screens, but the text field I want is located on a popup, which may be why it’s not working?

Thoughts?

Thanks in advance!

1 Like

The approach I use is to modify the textfield from Java. It definitely possible to access a control in a popup from Java, but it’s a bit different from a control in a screen, since a single popup could be instantiated many times. So you have to specify which popup.

When you create the popup, Nifty returns the Element instance that represents it. If you invoke findNiftyControl("textfield", TextField.class); on that instance (where “textfield” in this case is the id, not the control name) that should get you the textfield control you want to modify.

Can we mark this thread as resolved?

You can do what your trying to do but it has to look like this if your trying to get a return value from the screen controller. <control name="textfield" id="textfield" text="${CALL.getString()}" />. This will take whatever getString() returns and use that as its text value.

1 Like