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


#1

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!


#2

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.


#3

Can we mark this thread as resolved?