0
\$\begingroup\$

Im new to nifty and Im trying to change text control using nifty in JME application following this tutorial.

Below is my TextRenderer code:

Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
TextRenderer textRenderer = element.getRenderer(TextRenderer.class);
textRenderer.setText("loadingText");

Below is my nifty GUI code code for loadLevel scene:

<screen id="loadlevel" controller = "jmebase.TestLoadingScreen">
        <layer id="loadinglayer" childLayout="center" backgroundColor="#000000">
            <panel id = "loadingpanel" childLayout="vertical" align="center" valign="center" height="32px" width="70%">
                <control name="loadingbar" align="center" valign="center" width="100%" height="100%" />
                <control id="loadingtext" name="label" align="center" 
                         text="                                                  "/>
            </panel>
        </layer>
    </screen>

I printed value of for both element and textRenderer. element is initialised properly but textRenderer object is null. I googled lot and lots of people got this issue but none provided appropriate solution. Anyone know how to fix it. Im really stuck need help.

\$\endgroup\$
  • \$\begingroup\$ A Nifty Control is a composition, consisting of multiple basic elements. The loadingtext element is actually not a text element but it has a nested text inside. You should get the Label Control from loadingtext. It has a setText method. Sorry, i'm on my phone. I can elaborate later. \$\endgroup\$ – 1000ml Apr 19 '16 at 3:17
  • \$\begingroup\$ loadingtext element is label element and doesnt have setText method. so we get the TextRenderer wrapper for it which has setText method. But I dont know why it is null. Please post your elabotared answer. \$\endgroup\$ – Crawler Apr 19 '16 at 6:24
  • \$\begingroup\$ For the case of a Label my comment was wrong. Because a Label only consists of a single text element, the control tag effectively gets replaced by this text element. \$\endgroup\$ – 1000ml Apr 19 '16 at 13:12
1
\$\begingroup\$

getRenderer is going to return null if the element does not contain the class you're trying to retrieve. In this case, the element doesn't contain a TextRenderer, so it returns null.

You can avoid the error:

Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
if(element == null)
    return;

TextRenderer textRenderer = element.getRenderer(TextRenderer.class);
if(textRenderer == null)
    return;

textRenderer.setText("loadingText");

To get your expected result of setting the text, you need to ensure the element you're locating is properly configured.

\$\endgroup\$
  • \$\begingroup\$ element is properly initialised but textRenderer is still null. all code is from above tutorial. going through jme forum i found other dev also have same problem. I tried their solutions but it didnt work. Any ideas where I went wrong? \$\endgroup\$ – Crawler Apr 18 '16 at 5:44
  • \$\begingroup\$ Can't say for sure. The textRenderer isn't being initialized. Attach the debugger and step through the code. See where things are going wrong. \$\endgroup\$ – MichaelHouse Apr 18 '16 at 18:42
0
\$\begingroup\$

First off, the tutorial you linked is outdated. I also can't find any reference to Nifty in it.

I'm using Nifty 1.4 and cannot reproduce your problem. I can set the text via TextRenderer from the label.

However, you should avoid using renderers directly if you can achieve the same with a NiftyControl:

Element loadingtext = screen.findElementById("loadingtext");
Label label = loadingtext.getNiftyControl(Label.class);
label.setText("new text");

If this doesn't work for you, there might be a problem with your version of Nifty.

Also note: A text element is not the same as a label control. The Nifty manual explains this in detail: https://sourceforge.net/projects/nifty-gui/files/nifty-gui/1.3.2/nifty-gui-the-manual-1.3.2.pdf/download

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.