[Nifty GUI] Problem with changing screens using nifty.gotoScreen()


I have problem with changing screen when using nifty.gotoScreen(). It seems that bind is not called after executing nifty.gotoScreen(“start”).

That’s the method I use for changing screens in Main.java. It is executed after pressing ‘Done’ GUI button.

[java] public void doneSelecting() {
System.out.println(“Go to start screen”);

That’s the SelectController.java class handling bind() and listening selectionCHanged and DoneButtonClicked
[java]package mygame;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ButtonClickedEvent;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.controls.ListBox;
import de.lessvoid.nifty.controls.ListBoxSelectionChangedEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.List;

public class SelectController implements ScreenController, Controller {

private final Main app;

public SelectController(Main app) {
    this.app = app;

public void bind(Nifty nifty, Screen screen) {
    app.niftySelect = nifty;
    ListBox theBox = screen.findNiftyControl("colorSelectionBox", ListBox.class);
    for (String color : app.selections.keySet()) {
        System.out.println(color + " added !");
    System.out.println("Bind: " + nifty + " | " + screen.getScreenId());

@NiftyEventSubscriber(id = "colorSelectionBox")
public void onMyListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent<String> event) {
    List<String> selection = event.getSelection();
    System.out.println("Selected: " + selection.get(0));

@NiftyEventSubscriber(id = "doneButton")
public void onDoneButtonClicked(final String id, final ButtonClickedEvent event) {
    System.out.println("Done button clicked");

public void onStartScreen() {

public void onEndScreen() {


This is the .xml with gui

[java]<?xml version="1.0" encoding="UTF-8"?>

<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />

&lt;screen id="select" controller="mygame.SelectController"&gt;
    &lt;layer childLayout="center"&gt;
        &lt;panel id="panel" height="50%" width="30%" align="right" valign="top" childLayout="center" visibleToMouse="true"&gt;
            &lt;control name="label" 
                     text="Witaj wedrowcze... Tutaj zaczyna sie twoja przygoda. Chcialbys pewnie wiedziec skad sie tu wzieles?" 
                     align="left" valign="top"  
                     font="Interface/Fonts/Default.fnt" width="100%" height="100%" wrap="true"/&gt;
            &lt;control id="colorSelectionBox" width="90%" height="100%" align="center" 
                 valign="center" name="listBox" vertical="optional" horizontal="optional" 
                 displayItems="3" selectionMode="Single" 
                 forceSelection = "true" /&gt;
            &lt;control id="doneButton" name="button" label="Done" align = "center" valign="bottom" /&gt;

&lt;screen id="start" controller="mygame.SelectController"&gt;
  &lt;layer id="baseLayer" childLayout="center"&gt;
      &lt;control name="label2" id="GLabel2" width="716px" x="48" y="64" text="Oto drugi screen" height="33px" font="aurulent-sans-16.fnt" /&gt;


So after pressing Done button, GUI disappears and “start” screen is not shown.

There was already a thread on this forum with the similar stuff. I also had this problem with nifty and didn’t know how to solve it properly. Maybe if you search forum a bit, you will find some tip. If you do, please link it here also. :slight_smile:

since you extend Controller why don’t you use the state manager instead of gotoScreen(). for example:

public void goToSelectScreen(){
stateManager.attach(new SelectController());