Nifty: problems with showing/hiding panels

I have a panel with id=“main-container” and with childLayout=“horizontal”
In that panel I have 2 child panels, a panel with id=“confirm-panel” and another with id=“items-panel”
So here for clarity:

|-- confirm-panel
|-- items-panel

I want to be able to switch between that 2 child panels. Always only one of the two panels is visible.
So when I want to switch from “items-panel” to “confirm-panel”, I first call setVisible(false) on the items-panel, and then I simply call setVisible(true) on the confirm-panel. But it doesn’t work like I want it. When I show/hide a panel, the visibility is toggled indeed, but the layout does not ignore the invisible child panel. So there’s an empty space at the place where the items-panel was shown before it bacames invisible. And so the confirm-panel is pushed to the side. As it would be if both panels are visible.
I also tried the following:
screen.findElementByName(“main-container”).layoutElements(); // Layout child elements again after visibility changes

So I don’t only want to make an element invisible, but also want to completely ignore it.

You can give the parent container an overlay layout to force the two containers to overlap or you could physically remove and attach the gui children depending on which one is visible. As far as nifty is concerned hiding an element doesn’t remove it from the layout. It just toggles the visibility. Also, I would use “hide()” and “show()” instead of setVisible. That way you can apply animations or do other styling.

1 Like

Thank you! It helped me a lot.

But just another little question:
Is there a xml attribute like visible=“false” for nifty panels?

I’m really not sure. I’ve never been able to find a way to do that in xml so I tend to manually hide the elements I need to in my controllers. Not as clean as an xml setting but it works.

I have succeeded in showing and hiding the panels without layout problems. Now I have another problem that fits in this topic.
I’m using a overlay layout now for the parent panel.
The parent panel is in a window. When I hide the window and show it again (with my code), the panels become both visible. And they are drawn over each other (but that is normal in an overflow layout).

I think nifty makes the window and its content all visible when a window becomes visible.

It does. You will have to hide them manually when you re-open the window.