How can I manage the elements jme3 through nifty?

hi guy,

I want to create an app that work with textfield and button. Based on what I write in the textfield, when I press the button, the application reply.

I tried to create the text and button by Nifty and the app I inizializate in simpleInitApp(). Now, how can I manage the element jme3 through nifty? What is the best solution for my problem?? For you it is better to use the elements swing?


For you it is better to use the elements swing?

YAY! Never ;).

The best solution for this is to create your nifty controllers like app states, and attach them to the stateManager. Then your nifty controller will have access to the app and stateManager objects. Read this:

how do you convert an Element in a textfield?

for example, I have in element a text

Element niftyElement = nifty.getCurrentScreen().findElementByName(“red”);

now, in niftyElement, what is there? How do I do to appear a box of colour red, or blue, if I write in textfield blue?

Is it possible the casting from Element to String?


It is in manual :

Then instead of using the setter method use the getter.

The tutorial is this :

You can read the tutorials directly in jMP, just type F1.


I write an exercise-app to learn the comunication between a nifty element and a jme element. I set a number(String) that when arrive at 109 change the color at cube. but I want to change the color at the cube with a textfield( I write in textfield a color and the cube changes color). I have some problem to learn the tutorials because I am italian… Can anybody to alter my code of class ? If it is more light, I send all project…

thanks thanks thanks…


package tutorial;




import com.jme3.asset.AssetManager;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.NiftyMethodInvoker;

import de.lessvoid.nifty.controls.textfield.TextFieldLogic;

import de.lessvoid.nifty.elements.Element;

import de.lessvoid.nifty.elements.render.ImageRenderer;

import de.lessvoid.nifty.elements.render.TextRenderer;

import de.lessvoid.nifty.render.NiftyImage;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;

public class MyStartScreen extends AbstractAppState implements ScreenController {

private Nifty nifty;

Node pivot;

AssetManager assetManager;

private Application app;

private Screen screen;

int i=0;

public MyStartScreen(Node pivot, AssetManager assetManager) {

this.pivot = pivot;

this.assetManager= assetManager;


public void sayHello(String din){

System.out.println(“Nifty dice:”+din);

Element textfieeld = nifty.getCurrentScreen().findElementByName(“textback”);


//String qq = textfieeld.getRenderer(TextRenderer.class).getOriginalText();//nothing error, but doesn’t work! :frowning:

/this.textField.initWithText(textElement.getRenderer(TextRenderer.class).getOriginalText());// doesn’t work! :frowning:

//!!!run, but doesn’t go well!!!

String qw = textfieeld.getRenderer(TextRenderer.class).getWrappedText();

// String qw = textfieeld.initWithText(textfieeld.getRenderer(TextRenderer.class).getWrappedText());

System.out.println(“Nifty dice:”+qw);


catch(Exception e){

System.out.println(“error with textfield”);



public void gestionebutton(String myarg){

// find old text

Element niftyElement = nifty.getCurrentScreen().findElementByName(“testo1”);

// Element niftyElement = getElement().findElementByName("#testo1"); //test getElement()

// swap old with new text

//how it set in a string the new test that i insert in a textfield?then from string in int…it is easy!



String q = niftyElement.getRenderer(TextRenderer.class).getOriginalText();

System.out.println(q+" questo la label con il numero che incremento");

//String q = niftyElement.getRenderer(TextRenderer.class).getText();//nooooooooo

System.out.println("Nifty says "+myarg);//niftyElement.getRenderer(TextRenderer.class).getText());


System.out.println(“entrato nell’if”);

CuboAction cubo = new CuboAction(assetManager, pivot,“ciao”,-2f, 0f, 1f);


// load or create new image

NiftyImage img = nifty.getRenderEngine().createImage(

“Interface/yang.png”, false);

// find old image

Element niftyElement2 = nifty.getCurrentScreen().findElementByName(“playerhealth”);

// swap old with new image


//Similarly, to change the onClick() event of an element, create an ElementInteraction object:

//Element niftyElement3 = nifty.getCurrentScreen().findElementByName(“myElement”);

//maybe this is the right way…

//niftyElement3.getElementInteraction().getPrimary().setOnMouseOver(new NiftyMethodInvoker(nifty, “changejme()”, this));


public void startGame(String nextScreen) {

nifty.gotoScreen(nextScreen); // switch to another screen


public void quitGame() {



public String getPlayerName() {

return System.getProperty(“”);


* Nifty GUI ScreenControl methods /

public void bind(Nifty nifty, Screen screen) {

this.nifty = nifty;

this.screen = screen;


public void onStartScreen() {


public void onEndScreen() {


* jME3 AppState methods /


public void initialize(AppStateManager stateManager, Application app) { = app;



public void update(float tpf) {

if (screen.getScreenId().equals(“hud”)) {

Element niftyElement = nifty.getCurrentScreen().findElementByName(“score”);

1000/10 + “”); // fake score





Nifty Text Fields’ events works with EventBus, it means you have to subscribre your TextField by using the @NiftyEventSubscriber annotation. More details can be found here :


perhaps I much to ask, but is very difficult, is there somewhere a complete project? I am desperately…

how can I reward? thanks…! how does it set the string included in textfield in a string variable?

No. There aren’t any complete project, but there are a bunch of examples on nifty homepage…

Btw It’s very easy, Are you blind or can’t you read xD?

Put it in your nifty control :


@NiftyEventSubscriber(id = “score”)

public void onScoreChanged(final String id, final TextFieldChangedEvent event) {

TextField score = event.getTextFieldControl();

String text = event.getText();



The same thing for the others controls which supports EventBus. You can find the controls which supports EventBus in link above. Here is how you do it for Dropdowns:


@NiftyEventSubscriber(id = “myDropdown”)

public void onMyDropdownSelectionChanged(final String id, final DropDownSelectionChangedEvent<MyDropDownModel> event) {

MyDropDownModel selected = event.getSelection();

//do something with your drop down model



Aditionally, you can create one event method for several controls which shares the same control class. Like this:


@NiftyEventSubscriber(pattern = “.*TextField”)

public void onTextFieldChanged(final String id, final TextFieldChangedEvent event) {

TextField someTextField = event.getTextFieldControl();

String text = event.getText();

//You will know how is the text field by the id