Nifty + Listbox not showing

SO I am trying to get a list box showing and it is not working.



I can get text to show up though. Below is my XML any help is appreciated.



[xml]<?xml version="1.0" encoding="UTF-8"?>

<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">

<!-- Your IDE now tells you that one <screen></screen> element is expected here, etc. -->

<screen id="start" controller="mygame.Main">

<layer id="layer" backgroundColor="#0000" childLayout="vertical">

<panel id="panel" height="100%" width="100%" align="center" valign="center" backgroundColor="#999999" childLayout="vertical" visibleToMouse="true">

<!–interact onClick="quit()"/–>

<effect>

<onStartScreen name="fade" mode="in" length="300" startDelay="0" inherit="true"/>

<onEndScreen name="fade" mode="out" length="300" startDelay="0" inherit="true"/>

</effect>

<control id="serverListBox" name="listBox" vertical="On" horizontal="off" align="center" valign="center" displayItems="10" selection="Single" />

<text id="text" font="aurulent-sans-16.fnt" color="#000f" text="Hello from jME3" align="center" valign="center" />



</panel >

</layer>

</screen>

<screen id="end">

</screen>

</nifty>[/xml]

You listbox has no data? What should it show?

Show us the code in your bind method.

As requested here is my code. As you can see I am calling a method called fillMyListBox(); to load the data.



Normen. If you look at any html form or mfc or sping, when you define a listbox it should show at least empty. So if Nifty does not do that by deault, good to know.





[java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.NiftyEventSubscriber;

import de.lessvoid.nifty.controls.ListBox;

import de.lessvoid.nifty.controls.ListBoxSelectionChangedEvent;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;

import java.util.List;





/**

  • test
  • @author normenhansen

    */

    public class Main extends SimpleApplication implements ScreenController{



    private Nifty nifty;



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    Box b = new Box(Vector3f.ZERO, 1, 1, 1);

    Geometry geom = new Geometry("Box", b);



    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    mat.setColor("Color", ColorRGBA.Blue);

    geom.setMaterial(mat);



    rootNode.attachChild(geom);

    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,

    inputManager,

    audioRenderer,

    guiViewPort);

    nifty = niftyDisplay.getNifty();

    nifty.fromXml("Nifty/WorldServerListMenu.xml", "start",this);

    fillMyListBox();

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

    guiViewPort.addProcessor(niftyDisplay);



    // disable the fly cam

    // flyCam.setEnabled(false);

    // flyCam.setDragToRotate(true);

    inputManager.setCursorVisible(true);

    }

    public void fillMyListBox() {

    Screen screen = nifty.getScreen("start");

    ListBox listBox = screen.findNiftyControl("serverListBox", ListBox.class);

    listBox.addItem("a");

    listBox.addItem("b");

    listBox.addItem("c");

    }



    /**
  • When the selection of the ListBox changes this method is called.

    */

    @NiftyEventSubscriber(id="serverListBox")

    public void onMyListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent<String> event) {

    List<String> selection = event.getSelection();

    for (String selectedItem : selection) {

    System.out.println("listbox selection [" + selectedItem + "]");

    }

    }

    public void bind(Nifty nifty, Screen screen) {

    System.out.println("bind( " + screen.getScreenId() + ")");

    }



    public void onStartScreen() {

    System.out.println("onStartScreen");

    }



    public void onEndScreen() {

    System.out.println("onEndScreen");

    }



    public void quit(){

    nifty.gotoScreen("end");

    }





    @Override

    public void simpleUpdate(float tpf) {

    //TODO: add update code

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }

    [/java]

Well does it work now? Theres also an example template for listbox in the SDK. Press File->New and select the template under “GUI”.

by the way you should initialize your listBox in bind() method.

It is still not working :frowning:



Thanks Normen, I did not see a listbox option under there though, however that is a cool feature.



glaucomardano, Can you please explain what you mean by a bind() method?

Try moving the call to fillMyListBox to the bind method. Normally, I fill my listboxes in the bind method of my screen controller.

Oh, and store a reference to your listbox in a class variable. This is a lot faster then looking up the listbox each time you need it.



PS.

If I can find the time, I can check your code tonight when I get home, if you want. In preperation to that, do you see any errors in your log when running this code?

Okay, I moved it to the bind.



I set a break point and my listbox is still null after adding the 2nd item?



No, There are no errors running the code.

Here is the code from the nifty-default-controls-examples project:



http://nifty-gui.git.sourceforge.net/git/gitweb.cgi?p=nifty-gui/nifty;a=blob_plain;f=nifty-examples/src/main/java/de/lessvoid/nifty/examples/defaultcontrols/listbox/ListBoxDialogController.java;hb=refs/heads/1.3.1



This is a general screencontroller used with a listbox. It has a bit of extra stuff in there as well, since the example has a few text fields and buttons besides the listbox. But it should show you how to do the whole listbox setup / adding of items.

Figured it out through trial and error!



I needed to add:



[xml]

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

<useControls filename="nifty-default-controls.xml" />[/xml]



to my xml… since I had no default controls… Thanks ffor your help.



:slight_smile:





Cheers,



Tim

Gotta love when it is the simplest thing.

Ah yes, that would help…

Should have seen that

Thats what happens when you start with a basic tutorial and want to add objects in. Oh well Im rocking nifty now got a few interactive pages done!

Cool, gratz

and if you run into any problems let us know.



And if you have any controls you would like to see added to the default control, you can also let us know and me and Void will see what we can do.

I did a progress bar control with horizontal and vertical direction :), and with text too :). But the TextRenderer.java just render the text horizontally:







EDIT: Ops, this image doesn’t has text, I’m gonna change this image.

That’s it :):



Could you use an image instead of text?



An ugly hack would be to create a new line for each letter



H

E

A

L

T

H

Yes, it’s possible to use image, but I want to do it dinamically, because the text might be changed. e.g.: COMBO - 30. SCORE - 70000.

And about keeping blank spaces between the letters doesn’t work. The only way is to create a new label control and a new text renderer (Or adapt the existing ;)).