[SOLVED] NiftyGUI - When creating components and adding interacts from code makes the onclick effects not to work

Hi,

In niftygui I’m creating text from java code using a style that defines some effects. If I set onclick interaction, the onclick effect doesn’t run.

This is the snippet:

style:

  <style id="menu-item">
    <attributes align="center" valign="center" textHAlign="center" color="#fd0f" width="100%" font="Interface/fonts/barbatrick-30-3-8-0.fnt" 
                focusable="true" visibleToMouse="true"
                controller="de.lessvoid.nifty.controls.MenuItemControl"
                inputMapping="de.lessvoid.nifty.input.mapping.MenuInputMapping"
                />
    <effect>
      <onHover name="focus" />
      <onFocus name="textColor" color="#ff0f" />
      <onFocus name="textSize" startSize="1.0" endSize="1.25" length="150" />
      <onLostFocus name="textSize" startSize="1.25" endSize="1.0" length="150" />
      <onClick name="playSound" sound="menuSelect" />
    </effect>
  </style>

Java code:

TextCreator lblCreator=new TextCreator("");
lblCreator.setStyle("menu-item");

for(Data data : dataArray)
{
    lblCreator.setId(data.getId());
    lblCreator.setText(data.getName());
    lblCreator.setInteractOnClick("doSomethingWithData(" + data.getId() + ")");
    lblCreator.create(niftyDisplay.getNifty(), niftyDisplay.getNifty().getCurrentScreen(), parent);
}

If I just remove the setInteractOnClick line, the sound is played, but obviously the desired action is not done.

I’ve tried also to add the effect from code like lblCreator.addEffectsOnClick(new ControlEffectAttributes() setName setAttribute…with the same result, if setting the interaction the onclick effect doesn’t work

But if I define the text in xml like

  <text id="id1" text="Label1" style="menu-item" height="25%">
    <interact onClick="doSomethingWithData(test)" />
  </text>

it works as expected, but the data is dynamic, so I need to create the text in code

Thanks for any help

My first guess would be that the controller isn’t being properly set. Try setting it directly like:

ElementInteraction ei = element.getElementInteraction();
ei.getPrimary().setOnClickMethod(new NiftyMethodInvoker(nifty, "doSomethingWithData(" + element.getId() + ")" , ctrl));

Thanks for your comment @glh3586 but sadly it doesn’t work also. Removing the lblCreator.setInteractOnClick and setting the interaction like you said (element.getElementInteraction().getPrimary().setOnClickMethod(…)) behaves the same, the onclick effect doesn’t run :frowning:

Well without seeing your code I can’t really help you more. It looks like a controller problem from what you’ve said. I would step through the nifty source for interactions and check it out and see what it’s trying to call and on what controller.

As you say, I’ll try to follow niftygui code just in case I find it. Also I’ll create a simple test app to check if it may be a nifty issue or it’s related to my code and publish it later

Thanks

1 Like

It was my fault, I was removing all dynamic content in the callback avoiding the effect to be run…

Thanks for the comments :wink: