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();
	AppSettings settings = new AppSettings(true);
	settings.setResolution(1280, 720);

public void simpleInitApp() {

public void simpleUpdate(float tpf) {

public void simpleRender(RenderManager rm) {

private void setupGui() {
    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
    nifty = niftyDisplay.getNifty();
    nifty.fromXml("Interface/newradiobutton.xml", "start", this);

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

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

And here is my corresponding xml file.

<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns=""
  <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">
            <onActive name="border" color="#0008" />
          <panel childLayout="horizontal">
            <control name="label" text="Option 1" with="60px" />
            <control name="radioButton" id="option-1" group="RadioGroup-1" />
          <panel childLayout="horizontal">
            <control name="label" text="Option 2" with="60px" />
            <control name="radioButton" id="option-2" group="RadioGroup-1" />
          <panel childLayout="horizontal">
            <control name="label" text="Option 3" with="60px" />
            <control name="radioButton" id="option-3" group="RadioGroup-1" />
          <panel childLayout="horizontal">
            <control name="label" text="Option 4" with="60px" />
            <control name="radioButton" id="option-4" group="RadioGroup-1" />

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) ^^