NiftyEventSubscriber for radio buttons not firing

I am trying to get the simple radio button example to work and I can’t get the onRadioGroup1Changed method to fire. here is my code:

public class Main extends SimpleApplication implements ScreenController {
    public Nifty nifty;
    public static void main(String[] args) {
	Main app = new Main();
	app.setDisplayStatView(false);
	app.setShowSettings(false);
	AppSettings settings = new AppSettings(true);
	settings.setFullscreen(false);
	settings.setResolution(1280, 720);
        settings.setFrameRate(60);
	app.setSettings(settings);
	app.start();
}

@Override
public void simpleInitApp() {
    setupGui();	
}

@Override
public void simpleUpdate(float tpf) {
}

@Override
public void simpleRender(RenderManager rm) {
}

private void setupGui() {
    getFlyByCamera().setEnabled(false);
    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
            inputManager,
            audioRenderer,
            guiViewPort);
    nifty = niftyDisplay.getNifty();
    nifty.fromXml("Interface/newradiobutton.xml", "start", this);
    guiViewPort.addProcessor(niftyDisplay);
    inputManager.setCursorVisible(true);
}

@NiftyEventSubscriber(id="RadioGroup-1")
public void onRadioGroup1Changed(final String id, final RadioButtonGroupStateChangedEvent event) {
    System.out.println("RadioButton [" + event.getSelectedId() + "] is now selected. The old selection was [" + event.getPreviousSelectedId() + "]");
}

@Override
public void bind(Nifty nifty, Screen screen) { }
@Override
public void onStartScreen() { }
@Override
public void onEndScreen() { }
}

And here is my corresponding xml file.

<?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" />

  <screen id="start">
    <layer childLayout="center">
      <panel childLayout="horizontal" align="center" valign="center" width="50%" height="50%" backgroundColor="#55a5" padding="10">
        <control id="RadioGroup-1" name="radioButtonGroup" />
        <control name="label" text="Radio Buttons" />
        <panel width="10" />
        <panel childLayout="vertical" backgroundColor="#8001" paddingLeft="7px" paddingRight="7px" paddingTop="4px" paddingBottom="4px" width="105px">
          <effect>
            <onActive name="border" color="#0008" />
          </effect>
          <panel childLayout="horizontal">
            <control name="label" text="Option 1" with="60px" />
            <control name="radioButton" id="option-1" group="RadioGroup-1" />
          </panel>
          <panel childLayout="horizontal">
            <control name="label" text="Option 2" with="60px" />
            <control name="radioButton" id="option-2" group="RadioGroup-1" />
          </panel>
          <panel childLayout="horizontal">
            <control name="label" text="Option 3" with="60px" />
            <control name="radioButton" id="option-3" group="RadioGroup-1" />
          </panel>
          <panel childLayout="horizontal">
            <control name="label" text="Option 4" with="60px" />
            <control name="radioButton" id="option-4" group="RadioGroup-1" />
          </panel>
        </panel>
      </panel>
    </layer>
  </screen>
</nifty>

I can’t see how I am doing anything different from the example and yet I can’t get the corresponding method to fire when I change radio buttons. The radio buttons change color when I click them, but the method doesn’t work. Any ideas?

Look at the log, the nifty will town some exception at the app log and you will be able to see what is going on.
If not, just post the log here and we help you.
Anyway, just looking at your xlm, it dosent seens to be configurated to call anything

It’s because you never actually set a screen controller. Your “onRadioGroup1Changed” is never registered so the event is never actually subscribed to. The easiest way to fix that for your example would be to change your xml screen line to:

<screen id="start" controller="mygame.Main">

Of course you need to change “mygame” to the actual package Main is located in.

Also, as wagfeliz said… the log throws an error saying that there is no screen controller and that it is using the default one.

Thank you very much. Adding controller=“mypackage.Main” did it. It wasn’t clear to me from the wiki that I needed to connect the XML and the class this way. Just out of curiosity, is there a way to connect from the class instead of from the XML?

Try with nifty.registerScreenController(…)

Using nifty.registerScreenController only works if you want to replace what instance of a class is used as the screen controller i’m pretty sure. You can probably change what controller is used but I imagine it would be a little painful. I’ve never found a reason to do that so far.

There’s plenty of reason if you are like me and want to use the constructor to give a class instance access to whatever it needs. Say I want my screen controller to call a method on an instance of the class Foo. I would like to do: new ScreenController(foo)
But then I need to instantiate the screen controller myself and not let Nifty do it and that’s where registering controllers before the XML is loaded is great :smile:

I was more referring to setting them manually without setting it in the xml. You still have to set it in the xml first before you use nifty.registerScreenController. I apparently wasn’t very clear with that. That’s what I get for replying quickly while at work.

Then I agree completely :smile:

Agree. :smiley: The post above was just an example from my side how to set the screen controller. :wink: A nice example when to use registerScreenController(…) is if you restart your game and want to init a new instance of your screen controller class (for whatever reason) ^^