GUI button problems


im currently trying to understand jmonkey and im stuck with getting the Nifty gui working, here is the Code:

… //Starts in main


public class Main extends SimpleApplication {

public static void main(String[] args) {

new Main().start();


public void simpleInitApp() {

new Game("Erstes Game", (SimpleApplication) this);




… //travels to main menu


public class Game extends AbstractAppState {

private SimpleApplication app;

private String name;

public Game(String string, SimpleApplication app) { = name; = app;

MainMenu mainMenu = new MainMenu("Main Menu",app);





… //Displays the gui


public class MainMenu extends AbstractAppState implements ScreenController {

private NiftyJmeDisplay niftyDisplay;

private Nifty nifty;

private SimpleApplication app;

public MainMenu(String name, SimpleApplication app) { = app;



niftyDisplay = new NiftyJmeDisplay(

app.getAssetManager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());

nifty = niftyDisplay.getNifty();

//xml file and template

nifty.fromXml("Interface/GameGui.xml", name);



public void startGame() {

System.out.println("Start Game");


public void quitGame() {

System.out.println("Quit Game");


public void onAction(String name, boolean isPressed, float tpf) {



public void onStartScreen() {

System.out.println("On start screen");


public void onEndScreen() {

System.out.println("On end screen");


public void bind(Nifty nifty, Screen screen) {





None of the System.out.println Messages does show. Heres the XML:


… //XML

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

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

<screen id="Main Menu" controller="mygame.MainMenu">

<layer id="background" childLayout="center">

<image filename="Interface/lol.jpg" width="100%" height="100%"/>


<layer id="foreground" backgroundColor="#0000" childLayout="vertical">

<panel id="panel_top" height="25%" width="75%" align="center" childLayout="center"



<panel id="panel_mid" height="50%" width="75%" align="center" childLayout="center"


<panel id="panel_mid" height="50%" width="75%" align="center" childLayout="center">

<text text="Here goes some text describing the game and the rules and stuff. Incidentally,

the text is quite long and needs to wrap at the end of lines. …"

font="Interface/Fonts/Default.fnt" width="100%" height="100%" wrap="true" />



<panel id="panel_bottom" height="25%" width="75%" align="center" childLayout="horizontal"


<panel id="panel_bottom_left" height="50%" width="50%" valign="center" childLayout="center"


<control name="button" label="Start" id="StartButton" align="center" valign="center" visibleToMouse="true" >

<interact onClick="startGame()"/>



<panel id="panel_bottom_right" height="50%" width="50%" valign="center" childLayout="center"


<control name="button" label="Quit" id="QuitButton" align="center" valign="center" visibleToMouse="true" >

<interact onClick="quitGame()"/>








The Package is mygame.

The gui is displayed but the buttons doesnt work. Wat do?



You gotta supply the screenController (“this” in this case) to the nifty instance.

1 Like
You gotta supply the screenController (“this” in this case) to the nifty instance.

Thanks for the quick reply but im Totally clueless how to do this :(

nifty.addScreen(name, this); is not possible since MainMenu is an AbstractAppState and nifty.addScreen(name, nifty.getCurrentScreen()); doesnt work.

nifty.fromXml(“Interface/GameGui.xml”, name, this);

never mind. thanks.