Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get Nifty GUI to work with Slick2D. So far everything is going great, except that I can't seem to figure out how to properly interact with the GUI. I'm trying the example in the nifty manual http://sourceforge.n....0.pdf/download but it doesn't seem to entirely work.

The Element controller is being called for bind(...), init(...) and onStartScreen() as it should, as I can see their println output, but the next() method isn't being called when I click on the GUI element that I assigned the controller to, nor the screen controller as no output from println is shown.

What's weird is, that the player is moving, so the mouse input is working. It's supposed to be called when I click the mouse button on it from the in the XML.

Here is my code:

My Element controller:

public class ElementController implements Controller {

private Element element;

public void bind(Nifty nifty, Screen screen, Element element, Properties parameter, Attributes controlDefinitionAttributes) {
  this.element = element;
  System.out.println("bind() called for element: " + element);

public void init(Properties parameter, Attributes controlDefinitionAttributes) {
  System.out.println("init()  called for element: " + element);

public void onStartScreen() {
  System.out.println("onStartScreen()  alled for element: " + element);

public void onFocus(boolean getFocus) {
  System.out.println("onFocus()  called for element: " + element + ", with: " +

public boolean inputEvent(NiftyInputEvent inputEvent) {
  return false;

public void next() {
  System.out.println("next() clicked for element: " + element);


class MyScreenController implements ScreenController {
    public void bind(Nifty nifty, Screen screen) {}
    public void onEndScreen() {}
    public void onStartScreen() {}
    public void next() {
     System.out.println("next() called from MyScreenController");

And my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="" xmlns:xsi=""xsi:schemaLocation="">
<screen id="start" controller="predaN00b.theThing.V0004.MyScreenController">
  <layer childLayout="center" controller="predaN00b.theThing.V0004.ElementController">
   <panel width="100px" height="100px" childLayout="vertical" backgroundColor="#ff0f">
     <text font="aurulent-sans-16.fnt" color="#ffff" text="Hello World!">
      <interact onClick="next()" />

My main class, in case it's needed:

public class MainGameState extends BasicGame {

public Nifty nifty;

public MainGame() {

public void init(GameContainer container, StateBasedGame game) throws SlickException {

  nifty = new Nifty(new SlickRenderDevice(container), new NullSoundDevice(), new PlainSlickInputSystem(), new AccurateTimeProvider());



public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {

public void render(GameContainer container, StateBasedGame game, Graphics graphics) throws SlickException {

  public static void main(String[] args) throws SlickException {
     AppGameContainer app = new AppGameContainer(new MainGame());
     app.setDisplayMode( 1260 , 720, false); //window size
share|improve this question
+1 for what looks like a nicely asked question on your first time :) – James Feb 9 '12 at 17:47
Why thank you! I hope I get answers x3 – Patokun Feb 10 '12 at 7:56

Sorry, I do not keep track on the quests asked here. For support on the Nifty-GUI and on Slick best use the boards of Slick2D and Nifty-GUI to ask your questions.

How ever in case your question is still relevant, you shall get your answer.

To enable the Nifty-GUI to receive mouse and keyboard you have to forward all events arriving in Slick2D to the Nifty-GUI. This is a pretty major task as Slick2D and Nifty-GUI have separated polling times for this events. So you basically have to buffer the events from Slick2D and forward them to Nifty-GUI upon request.

How ever you are using the PlainSlickInputSystem that basically has this functionality already included. In order to hook it into Slick properly you need to do the following:

public void init(GameContainer container, StateBasedGame game) throws SlickException {
 SlickInputSystem inputSys = new PlainSlickInputSystem();

 nifty = new Nifty(new SlickRenderDevice(container), new NullSoundDevice(), inputSys , new AccurateTimeProvider());



Once you did so the PlainSlickInputSystem will take care for relaying the input events from Slick to Nifty.

How ever I propse that you make use of the Game, BasicGame, GameState and BasicGameState implementations that are delivered along with the Slick2D-render library for Nifty. Those classes implement the default Slick classes with there respective names and add all functionality to display and use the Nifty-GUI. The entire initialization is done more or less automatically. Updating and rendering is done automatically as well.

Please also take a look at the Nifty-Slick2D-Renderer reference page for further details on how use use this renderer in the best way.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.