Building Panels

I’m currently messing around with my game’s onStartScreen method with Nifty. I want to build out a list as a scroll-able panel of rows where each row is a panels that is populated during the onStartScreen method. I can get a handle to the panel I’d like to scroll using screen.findElementByName(), but I’m having difficulty building the rows and then adding them to that panel. I can’t find any documentation that describes adding panels to the screen during the onStartScreen method. Does anyone know how I’d go about adding panels dynamically like this?

Thanks!

Well you could simply add the panels via external method. Here´s an example (this example adds n new panels including a button for each new panel):

    private int idCountIn;
    private int idCountOut;
    
    private Screen screenTest;
    private Element parentPanel;
    private PanelCreator createPanel1, createPanel2;
    private ButtonBuilder createButton;

    public void onStartScreen(){
        
        initPanel();
    }

    public void initPanel(){
            for(int i = 0; i < amount_of_panels; i++){
                this.addPanel();
            }
        }
    }
    
    public void addPanel(){
        screenTest = nifty.getCurrentScreen();
            
        parentPanel = screenTest.findElementByName("Panel-Box");
        createPanel1 = new PanelCreator();
        createPanel1.setId("panel-" + idCountOut + "-Content-" + idCountIn);
        createPanel1.setHeight("20%");
        createPanel1.setWidth("100%");
        createPanel1.setBackgroundColor("#f00f");
        createPanel1.setChildLayout("center");
        createPanel1.setStyle("nifty-panel-simple");
        createPanel1.create(nifty, screenTest, parentPanel);
        idCountIn++;
            
        parentPanel = screenTest.findElementByName("panel-" + idCountOut + "-Content-" + (idCountIn-1));
        createPanel2 = new PanelCreator();
        createPanel2.setId("panel-" + idCountOut + "-Content-" + idCountIn);
        createPanel2.setHeight("50%");
        createPanel2.setWidth("50%");
        createPanel2.setBackgroundColor("#0000FF");
        createPanel2.setChildLayout("center");
        createPanel2.setStyle("nifty-panel-simple");
        createPanel2.create(nifty, screenTest, parentPanel);
        idCountIn++;
            
        parentPanel = screenTest.findElementByName("panel-" + idCountOut + "-Content-" + (idCountIn-1));
        createButton = new ButtonBuilder("panel-" + idCountOut + "-Content-" + idCountIn);
        createButton.height("80%");
        createButton.width("80%");
        createButton.focusable(true);
        createButton.label("Button " + idCountOut);
        createButton.build(nifty, screenTest, parentPanel);
        idCountIn++;
        
        idCountOut++;
        idCountIn = 0;
    }

I hope this solves your problem. :slight_smile: