Setting text to a textfield in nifty problem

Hello!

I am currently creating a menu for my game with Nifty GUI and ran in to a problem.

I can get text from a textfield in Nifty and print it to the console, but i cannot setText to textfield in nifty, nothing happens.
I get the textfield in the controller like this:

TextField fpsTextField = nifty.getCurrentScreen().findNiftyControl("textFps", TextField.class);

And for getting the text I do:

fpsTextField.getRealText();

But when I do this for setting the text, nothing happens:

fpsTextField.setText("hi");

Any idea what could be wrong?

Using Nifty-editor 0.5.9.jar.

You need to get the text renderer not the control in order to set the text. Off the top of my head, it should look something like this:

element.getRenderer(TextRenderer.class).setText("my text");

Thanks for answering, but I get syntax error. But this is weird.

When i load my view I try to set text like this but it DOES NOT work:

public void options() {
        //nifty.fromXml("Interface/Menu2new.xml", "GScreen2", this);
        nifty.gotoScreen("GScreen2");

        //ConfigurationModel configurationModel = ConfigurationManager.getInstance().getConfigurationModel();

        TextField fpsText = nifty.getCurrentScreen().findNiftyControl("textFps", TextField.class);
        TextField resX = nifty.getCurrentScreen().findNiftyControl("resX", TextField.class);
        TextField resY = nifty.getCurrentScreen().findNiftyControl("resY", TextField.class);
        CheckBox soundCheck = nifty.getCurrentScreen().findNiftyControl("soundCheck", CheckBox.class);

        
        //THIS DOESNOT WORK WHEN I LOAD THE "OPTION VIEW"
        TextField t = nifty.getCurrentScreen().findNiftyControl("textFps", TextField.class);
        t.setText("hej");

    }

But when i press my save button, it sets the text on the textfield…

public void save() {
        ConfigurationModel configurationModel = new ConfigurationModel();

        TextField fpsText = nifty.getCurrentScreen().findNiftyControl("textFps", TextField.class);
        System.out.println(fpsText.getRealText());
        TextField resX = nifty.getCurrentScreen().findNiftyControl("resX", TextField.class);
        TextField resY = nifty.getCurrentScreen().findNiftyControl("resY", TextField.class);
        System.out.println(resX.getRealText() + " " + resY.getRealText());
        CheckBox soundCheck = nifty.getCurrentScreen().findNiftyControl("soundCheck", CheckBox.class);
        System.out.println(soundCheck.isChecked());

        // BUT THIS WORKS WHEN I PRESS THE SAVE BUTTON
        TextField t = nifty.getCurrentScreen().findNiftyControl("textFps", TextField.class);
        t.setText("hej");
} 

The code is the same, what could be the problem?

I tried this but it didn’t work:

Element e = nifty.getCurrentScreen().findElementByName("textFps");
        e.getRenderer(TextRenderer.class).setText("ASD");

Sorry, misread it and thought you were using labels. I use exactly what you have in your first post to set textfields in my game without any issue.

Looking at your second post on the code, it looks like your issue is that you are trying to set the text during transition. You call the go to screen command but that doesn’t transition the screen immediately. That is why it works for your save but not your options call. You can set the text in the “start screen” section in the controller that you have assigned to “GScreen2”.

Ok then I understand! Everything works now when I put it in the onStartScreen() method!

Thank you very much!! :smile: