Understandig the use of nifty, screencontroller and appstates

Hi Guys,

im new to java an jmonkeyengine. Before i start with jmonkey i learned programming with php, vb.net and c++. Im a Rookie in programming, but understand the basics. First at all i have to say im not really good in English.

A few days ago i import a blender model and was able to build the code and show the result. But before i want to begin with 3d models, terrains and other things i want to learn how to use the nifty gui library. I created a nifty gui with xml and was able to show the result. Now i have the problem to integrate interactions with jmonkey and nifty.

I read the tutorials and understand for what appstates and screencontrollers are. screencontrollers helps me for the interactions and with switching appstates im possible to change inputactions when the user press the same key.

My Problem now is to put the example codes from the tutorials together. In the Tuorials are Classes with methods and i dont know why there is no example how to use the classes with the mainloop together. I created a class like this

public class Appstatestartmenue extends AbstractAppState implements ScreenController {


What i have to do now? My first try was to create an instance of Appstartmenue in the mainloop and deliver the nifty gui object to the class. Now i must be able to write methods to do something with the delivered object. Maybei misunderstood something, because i handle appstate and a screen with the same class.

Maybe anyone has a good example code with screens, appstates and nifty.

You need to register your screen controller with nifty.

Something like (pseudo code)


create nifty;

Appstatestartmenue state = new Appstatestartmenue();



do more initializing


You should read the java coding conventions style guides too: http://www.oracle.com/technetwork/java/codeconv-138413.html

They help people read each other’s code.

Days later…

I have now a Code but the button event idnt work. My Code is below…

Codesnippet from startmenue.xml

[xml]<control name="button" label="Spiel beenden" id="spielbeenden" valign="center" align="center"


<interact onClick="quitgame()" />


My Class


package mygame.screen;

import com.jme3.app.Application;

import com.jme3.app.SimpleApplication;

import com.jme3.app.state.AbstractAppState;

import com.jme3.app.state.AppStateManager;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



  • @author ben


    public class Appstatestartmenue extends AbstractAppState implements ScreenController {

    private Nifty nifty;

    private Screen screen;

    private SimpleApplication app;

    public void Appstatestartmenue() {


    * Nifty GUI ScreenControl methods /

    public void bind(Nifty nifty, Screen screen) {

    this.nifty = nifty;

    this.screen = screen;


    public void onStartScreen() {


    public void onEndScreen() {


    public void quitgame() {



    * jME3 AppState methods /


    public void initialize(AppStateManager statemanager, Application app) {

    super.initialize(statemanager, app);




    public void update(float tpf) {

    * jME update loop! */




    my mainfile

    [java]public class Main extends SimpleApplication {

    private Appstatestartmenue startmenuestate;

    public static void main(String[] args) {

    Main app = new Main();




    public void simpleInitApp() {

    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

    assetManager, inputManager, audioRenderer, guiViewPort);

    Nifty nifty = niftyDisplay.getNifty();

    startmenuestate = new Appstatestartmenue();

    nifty.fromXml(“Interface/startmenue.xml”, “start”, new Appstatestartmenue());



    startmenuestate.bind(nifty, nifty.getScreen(“start”));

    startmenuestate.initialize(stateManager, this);





    public void simpleUpdate(float tpf) {

    //TODO: add update code



    public void simpleRender(RenderManager rm) {

    //TODO: add render code



    I can build the Code but the click event on the button “spielbeenden” didnt work.

You created two different Appstatestartmenue … try doing what I put in my psuedo code :wink:

I have now this Code

[java]NiftyJmeDisplay niftydisplay = new NiftyJmeDisplay(

assetManager, inputManager, audioRenderer, guiViewPort);

Nifty nifty = niftydisplay.getNifty();

startmenuestate = new Appstatestartmenue();



nifty.fromXml("Interface/startmenue.xml", "start");

Screen screen = nifty.getCurrentScreen();

startmenuestate.bind(nifty, screen);

startmenuestate.initialize(stateManager, this);



I searched for other example Codes in the Forum, but there are to much differents, so i didnt understand how to initialize my code. I think im close to get it work, but something is missing.

i create a niftyobject and ad the niftygui screen to it

added my class to the statemanager

bind nifty and the screen to my class

initialize the statemanager and the app object to my class

and add niftydisplay to the guiviewport.

whats wrong?

Don’t call bind or initialize yourself. The system does that.

You certainly look as though you are closer.

Read this (and the other related tutorials too) and it might help:


Omg it works. While i was coding i changed the name of my Screencontroller Class and didnt changed the value in the Xml File. Im an idiot sorry. But i have one question before we close this Thread. You write that the system initialize the methods in the Class. Is that a Javafeature? I want to read more about it before i read the next tutorials. So how the cool feature calls? :slight_smile:

Nifty calls bind for you before it uses the screen controller. The State Manager calls initialize when you attach and enable the state…

ok thanks didnt know that