Dynamically adding controls to a popup

Hi All,



I’ve been building a popup menu in nifty but I want to dynamically add items to the menu



I’m doing this using:



[java] popup = nifty.createPopup(“popupCardMenu”);

nifty.showPopup(nifty.getCurrentScreen(), popup.getId(), null);

popup.startEffect(EffectEventId.onCustom, null, “show”);



Element panel = popup.findElementByName(“menuContents”);



for (final MenuItem item: menuItems) {

new TextBuilder() {{

text(item.getName());

backgroundImage(“Interface/MenuBackground.png”);

imageMode(“resize:7,1,7,1,7,1,7,1,7,1,7,1”);

}}.build(nifty, popup.nifty.getCurrentScreen(), panel);

}[/java]



I tried adding the textbuilder before doing the popup and nifty crashed out throwing a null pointer exception. I moved it after and it didn’t crash but it also added nothing to the popup…(I’ve tried specifying height and width in the textbuilder as well, made no difference).



Only work around I can think of at the moment is to create a batch of menu items in the XML in advance and then finding and editing those from the code but for obvious reasons that is undesirable.



Does anyone know how to make this work?



Thanks,

Zarch

Oh, or build the entire popup in code should work, I see there is a PopupBuilder.



I’d rather not as all my other screens and popups are in XML but maybe that’s the best way forwards.

Found this https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_popup_menu and with a bit of reskinning was able to get the result I needed :slight_smile:

1 Like