Text from Textfeild Nifty GUI


I am trying to do this safe combination thing where the user gets to type in the code in a textbox that pops up. I need to be able to use the input of the user and put it in a string. How can I do that?

My code:

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

assetManager, inputManager, audioRenderer, guiViewPort);

Nifty nifty = niftyDisplay.getNifty();
nifty.fromXml("Interface/SafeCode.xml", "Safe");



<useStyles filename="nifty-default-styles.xml" />
<useControls filename="nifty-default-controls.xml" />

<screen id="Safe">
    <layer childLayout="center">
        <panel width="25%" height="25%" align="center" valign="center" childLayout="vertical" backgroundColor="#55a5" padding="10">
            <control id = "input" name="textfield" />


There you go:

To get the textfield control just use: screen.findNiftyControl("control_ID", TextField.class); where the screen could be nifty.getCurrentScreen();

Thanks for the reply!

I’m sorry for being a noob, but where should i put what? When i put this code:

control(new TextFieldBuilder(“passwordCharTextField”, “") {{ // init with the text "
maxLength(1); // force only a single character input

it says that it cannot find the control method. Can you please give me a step to step guide on how to do this? I’m asking too much, but my whole game is just sitting on this right now.


Well… in the manual you have all you need to get it work, I strongly recommend you to read it all.

First of all you have to decide if you want to create the controls by code or with the xml. I’ll follow your first try with the xml:

So you have:

and you add it to nifty with:

Well. Now you have to go the screen “Safe” if you want to see it, obviously, for that do:

Screen screen = nifty.getCurrentScreen();
TextField textField = screen.findNiftyControl("input", TextField.class);
String text = textField.getText();

I think you can continue from here. However, read the manual and use custom controls if you want to have a better structure.