Nifty inventory HUD extremely slow

I’m using Nifty GUI to populate my HUD. I’m trying to implement an inventory similar to minecraft’s item slots at the bottom of the screen.

Every time the player picks up a new item the frame rate gets slower and slower until it’s not playable at about 5 items. I have a HUDControl that is my ScreenController.

Here is my onStartScreen method:

   public void onStartScreen()
    screen = nifty.getCurrentScreen();
    for (int col = 0; col < INVENTORY_COLUMNS; col++)
        panelBuilder = new PanelBuilder("");"hudItemColumn" + col);
        Element column =, screen, screen.findElementByName("hudItemSlots"));
        slotBuilder = new ControlBuilder("itemSlot");"itemSlot" + col);, screen, column);

Then in my controlUpdate method I populate the items from my Inventory data structure as follows:

    private void populateInventoryItems()
    List<InventoryItem> items = player.getInventory().getEquippedItems();
    for (int i = 0; i < items.size() && i < INVENTORY_COLUMNS; i++)
        InventoryItem item = items.get(i);
        itemBuilder = new ControlBuilder("item");"item" + i);

        final int spriteIndex = item.getBlock().getSpriteIndex();
        Element e =, screen, screen.findElementByName("itemSlot" + i));
        ImageBuilder imageBuilder = new ImageBuilder()
                imageMode("sprite:16,16," + spriteIndex);

        };, screen, e);

I’m sure a seasoned developer will know what is causing the problem. Should I have an InventoryListener that reports inventory changes to my HUDControl or am I going about this all wrong?

Thanks for the help!

Updating the gui like that every frame on update is your performance problem. You really only want to update when there is a change in your inventory.

Thanks @glh3586. That’s what I did.

I created an InventoryListener with an onInventoryChanged method that my HUDControl implements. It marks a dirty flag and rebuilds the inventory items when the flag is true.