Can't populate ListBox?

Hello. I’ll get straight to the point - I can’t populate the ListBox nifty gui panel with any data. I tried several other solutions presented in this forum but I still can’t seem to get it to work. The class that implements this GUI is an extension of abstractAppState class. Can anyone see anything wrong with this code?



[xml]

<?xml version=“1.0” encoding=“UTF-8”?>

<nifty>

<useControls filename=“nifty-default-controls.xml” />

<useStyles filename=“nifty-default-styles.xml” />

<screen id=“start”>

<layer id=“background” childLayout=“center”>

<image filename=“Interface/underground-background.jpg”></image>

</layer>

<layer id=“layerWithButtons” childLayout=“center” height=“75%”>

<panel id=“panelTop” height=“75%” width=“500px” align=“center” valign=“center” childLayout=“center” visibleToMouse=“true”>

<control id=“myListBox” name=“listBox” horizontal=“false” width=“75%” height=“100%” childLayout=“vertical” visibleToMouse=“true”>

<panel id=“myListBoxData” width=“100%” hwight=“100%” childLayout=“vertical” visibleToMouse=“true”>

<label text=“Static Item I” style=“my-listbox-item-style” />

<label text=“Fo Shizzle” style=“my-listbox-item-style” />

<label text=“Static Item III” style=“my-listbox-item-style” />

<label text=“Static Items ftw” style=“my-listbox-item-style” />

</panel>

</control>

</panel>



</layer>



</screen>

<screen id=“end”>

</screen>

</nifty>



[/xml]



and my Java looks like this:

[java]

package views;



import com.jme3.app.Application;

import com.jme3.app.SimpleApplication;

import com.jme3.app.state.AbstractAppState;

import com.jme3.app.state.AppStateManager;

import com.jme3.asset.AssetManager;

import com.jme3.audio.AudioRenderer;

import com.jme3.bullet.BulletAppState;

import com.jme3.font.BitmapFont;

import com.jme3.font.BitmapText;

import com.jme3.input.FlyByCamera;

import com.jme3.input.InputManager;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.renderer.ViewPort;

import com.jme3.scene.Node;

import com.jme3.system.AppSettings;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.controls.ListBox;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



/**

*

  • @author pawelgasiorowski

    /

    public class StartScreen extends AbstractAppState implements ScreenController {





    private SimpleApplication app;

    private Node rootNode;

    private AssetManager assetManager;

    private AppStateManager stateManager;

    private InputManager inputManager;

    private ViewPort viewPort;

    private BulletAppState bullet;

    private Node guiNode;

    private AppSettings appSettings;

    private AudioRenderer audioRenderer;



    private BitmapFont guiFont;

    private BitmapText hudText;



    private FlyByCamera flyByCamera;



    private int number = 0;



    @Override

    public void initialize(AppStateManager stateManager, Application app) {

    super.initialize(stateManager, app);

    this.app = (SimpleApplication) app;

    this.rootNode = this.app.getRootNode();

    this.assetManager = this.app.getAssetManager();

    this.stateManager = this.app.getStateManager();

    this.inputManager = this.app.getInputManager();

    this.viewPort = this.app.getViewPort();

    this.guiNode = this.app.getGuiNode();

    this.audioRenderer = this.app.getAudioRenderer();

    this.flyByCamera = this.app.getFlyByCamera();







    // nifty experiment

    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

    assetManager, inputManager, this.audioRenderer, this.viewPort);

    /
    * Create a new NiftyGUI object /

    Nifty nifty = niftyDisplay.getNifty();

    /
    * Read your XML and initialize your custom ScreenController */

    nifty.fromXml("Interface/screen.xml", "start");

    //nifty.setDebugOptionPanelColors(true);

    // nifty.fromXml("Interface/helloworld.xml", "start", new MySettingsScreen(data));

    // attach the Nifty display to the gui view port as a processor

    this.viewPort.addProcessor(niftyDisplay);

    // disable the fly cam

    this.flyByCamera.setDragToRotate(true);

    this.flyByCamera.setEnabled(false);



    }







    // SimpleUpdate

    @Override

    public void update(float tpf)

    {



    }



    public void bind(Nifty nifty, Screen screen) {

    ListBox listbox = nifty.getCurrentScreen().findNiftyControl("myListBox", ListBox.class);





    listbox.addItem("afasd");



    listbox.addItem("bafsd");



    listbox.addItem("fasdc");

    }



    public void onStartScreen() {



    }



    public void onEndScreen() {

    }

    }



    [/java]



    Any help most appreciate it!

Why do you have a panel inside your list box? I use controls and add items programmatically with no problems but I don’t have any extra stuff inside the list box declaration. (Actually now all the list boxes are created using builders but we did use to have some created from XML and it worked the same).

Hey zarch - thanks for the reply. I’m fairly new to jmonkey and nifty gui so please bear with me ^_^’. I’ve removed the panel from the list box just like you suggested - but still couldn’t get this to work. I can see the panel with two scrollbars but without any items inside it… Can you provide some sample code how would you do it “programatically”? This is what I have atm:



http://i.imgur.com/Kx529.jpg

Actually, zarch I’ve done it - thanks for the hint :wink:

Cool, glad it helped. :slight_smile:

Populating Listboxes from XML or from Builder was supported some time ago but is not anymore. You can only add items dynamically from Java using the Listbox API. This is because now the ListBox directly supports your own custom instances, e.g. you can add your own whatever object directly if you want. And this was not possible with XML/Builder anymore so support for this has been dropped.

1 Like

Yes, I drop my own objects into the list, very useful :slight_smile: