Text from Textfeild Nifty GUI

Hi,

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");


guiViewPort.addProcessor(niftyDisplay);

SafeCode.xml:

<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" />
            
        </panel>
    </layer>
</screen>

Cheers

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
width(“20px”);
}});

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.

Thanks

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:

nifty.gotoScreen("Safe");
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.