Dynamically remove elements (Nifty GUI)

I have been searching the forums for how to dynamically update my GUI during run time. I have found good info on how to add elements dynamically, but I haven’t found things about how to remove elements or possibly make them not visible any longer.



The scenario is that I have a status panel in my GUI and if I select a tile in my game I want to draw various elements(buttons, labels…ect) based on the type of tile the user selects.



This means I would like to remove all current existing elements in the panel and build up a new one.



Anyone have any suggestions about if this is possible?



Short questions:

  1. Can you remove elements dynamically
  2. Can you make existing elements no longer visible or disable them somehow.



    Thanks
  1. The Element class has a markForRemoval() method. Maybe this will help you.
  2. The same class also has a setVisible() method.

As myamo said you can remove them. If your layout makes trouble, navigate to the parent-element of the element you have removed and call a method that should be something like “layoutElements” or “refreshLayout”. Then the elements of this particular parent-element are rearranged.

You can do setVisible to remove things temporarily or markForRemove to remove them permanently.

Hi guys,
A wierd issue I am facing while removing elements and adding elements.
I have a panel with 20 elements and I want to remove them all and another 20 elements.
I have written an Controller implementation for this panel. Below is the code from that class

public void removeChildren(){
for(int i=0; i<element.getElements().size(); i++){ ///this has 20 size
element…getElements().get(i).markForRemoval();
}
//call to layout elements
element.layoutElements();
}

Now that panel is empty.
However later when I add another 20 items to that panel. It never shows on the screen
However, printing its getElements().size() gives 20 elements.
I am not sure where I am missing something to get this working.

Post your code to add them too.

Do you ever call both the add and the remove in the same frame?

1 Like

Well, the addition is immediately after I delete the elements.
Also, I implemented the EndNotify interface and actually added the new elements only after all the elements have been removed by nifty, which we get in the following method

public void perfor() — of the EndNotify interface.

Still, in this case also the new elements are not visible, though they are actually there in the panel.
Sorry, i m at office now, I will post the code once I get home, Thanks

Cool, got it working.
Actually have to wait till nifty clears all the elements
in the EndNotify interface implementation.

Then added new elements, Violla

Thanks for same frame hint, it clicked :slight_smile:

You can use this code:

[java]private void cleanActivities(){
for(Element tempElement : screen.findElementByName(“winPrC_Parent”).getElements()){
nifty.removeElement(screen, tempElement);
}
nifty.executeEndOfFrameElementActions();
}[/java]

it acts immediatly!!! because you are ending the “frame”.
with “markForRemoval” you have to wait for ending frame…

:wink: good luck