[Nifty] A simple listbox or scrollpanel --Solved

Hello,



I’m trying to use a simple list box on one of my UI.



When checking what is inside nifty-defaultcontrols.xml, I’ve found that we can use :

[xml]

<control id=“listCurrentDescription” name=“scrollPanel” height=“100%” width=“75%” horizontal=“false” vertical=“true” autoScroll=“true” />

[/xml]

But I’m clueless of how we can fill it.



I checked the example ChatArea on the wiki but : It doesn’t seem up to date ( The class ScrollPanel don’t have a method setAutoScroll for example )



So, if someone can give me a very small example of how to build and fill one, I will be reall gratefull.

Static Listbox example:



[xml]<control id=“listBoxStatic” name=“listBox” horizontal=“false” width="*" height=“100%” childLayout=“vertical” visibleToMouse=“true”>

<panel id=“listBoxStaticData” width=“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>[/xml]



Dynamic Listbox example:



http://nifty-gui.svn.sourceforge.net/viewvc/nifty-gui/nifty-examples/trunk/src/main/java/de/lessvoid/nifty/examples/controls/ControlsDemoStartScreen.java?revision=1118&view=markup



(Somwhere in there is a dynamic listbox example, where a listbox control is dynamically created and filled)

1 Like

Perfect!!!



Thanks a lot

I can’t make it work!



Since the control “listBox” doesn’t have a dropDown Control I used a listBoxControl instead :

[java]

ListBoxControl listBoxControl = screen.findControl( “listCurrentDescription”, ListBoxControl.class);

[/java]



The method additem exists but thorw a null pointer Exception :frowning:


java.lang.NullPointerException
at de.lessvoid.nifty.controls.dynamic.attributes.ControlAttributes.buildControl(ControlAttributes.java:333)
at de.lessvoid.nifty.controls.dynamic.attributes.ControlAttributes.createLabel(ControlAttributes.java:286)
at de.lessvoid.nifty.controls.dynamic.LabelCreator.access$000(LabelCreator.java:10)
at de.lessvoid.nifty.controls.dynamic.LabelCreator$1.createControl(LabelCreator.java:27)
at de.lessvoid.nifty.Nifty$ControlToAdd.createControl(Nifty.java:716)
at de.lessvoid.nifty.Nifty.addControlsWithoutStartScreen(Nifty.java:278)
at de.lessvoid.nifty.controls.dynamic.LabelCreator.create(LabelCreator.java:30)
at de.lessvoid.nifty.controls.listbox.controller.ListBoxControl.addItem(ListBoxControl.java:182)
at com.didigame.acariaempire.gui.start.NewGameController.initSunthilPresentation(NewGameController.java:143)
at com.didigame.acariaempire.gui.start.NewGameController.updateDescription(NewGameController.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at de.lessvoid.nifty.NiftyMethodInvoker.callMethod(NiftyMethodInvoker.java:164)
at de.lessvoid.nifty.NiftyMethodInvoker.performInvoke(NiftyMethodInvoker.java:115)
at de.lessvoid.nifty.Nifty$DelayedMethodInvoke.perform(Nifty.java:949)
at de.lessvoid.nifty.Nifty.invokeMethods(Nifty.java:927)
at de.lessvoid.nifty.Nifty.handleDynamicElements(Nifty.java:213)
at de.lessvoid.nifty.Nifty.processMouseEvent(Nifty.java:195)
at com.jme3.niftygui.InputSystemJme.forwardEvents(InputSystemJme.java:117)
at de.lessvoid.nifty.Nifty.render(Nifty.java:163)
at com.jme3.niftygui.NiftyJmeDisplay.postQueue(NiftyJmeDisplay.java:134)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:664)
at com.jme3.renderer.RenderManager.render(RenderManager.java:691)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:216)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:144)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:133)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:198)
at java.lang.Thread.run(Unknown Source)


In the case I've done something wrong :
Here is my xml part :
[xml]

control id="listCurrentDescription" name="listBox" horizontal="false" width="75%" height="100%" childLayout="vertical" visibleToMouse="true">












</control
[/xml]

My code :
[java]
ListBoxControl listBoxControl = screen.findControl( "listCurrentDescription", ListBoxControl.class);
listBoxControl.addItem( "That's my spot!");
[/java]
The control itself is not null but when I check the controlled elements, it seems there is no elements attached to it :(

I think the problem is, that the listBox control in the xml requires a child element that has the same id as the listbox with “Data” appended.



[xml]<control id=“listBoxStatic” name=“listBox” horizontal=“false” width="*" height=“100%” childLayout=“vertical” visibleToMouse=“true”>

<panel id=“listBoxStaticData” width=“100%” childLayout=“vertical” visibleToMouse=“true”>[/xml]



And yes, I agree that this is somewhat odd :slight_smile:

I still have some questions :slight_smile:



Now, I can add lines to my list :

[java]

Element parent = screen.findElementByName( “listCurrentDescription” );

ListBoxControl listBoxControl = screen.findControl( “listCurrentDescription”, ListBoxControl.class);

String text = formatMultiLineString( LanguageManager.getGameMessage( “startSunthilDescription” ), parent.getWidth() / 8 );

String lines = text.split(“n”);

for( int i = 0; i < lines.length; i ++ )

{

listBoxControl.addItem( lines );

}

[/java]



But my scroll sidebar is not updated, and I can’t display the last ones.

Is there a method to update the scroll sidebar?




I tried to use the method addElement to add an Image to the list
[java]
ImageCreator creator = new ImageCreator();
creator.setHeight( "50%" );
creator.setWidth( "100%" );
creator.setImageMode( "normal" );
creator.setFilename( "sunthil_picture.jpg" );
Element image = creator.create(nifty, screen, parent);
listBoxControl.addElement( image );
[/java]
But it fails.
Do I use the good method to create an element ?
Should I store the image in an hidden panel and link it to the list after that ?

Thanks for your answers unitl now :)

Edit :
For the ImageCreator, I've now understood that the parent must be the 'listBoxData' and not the 'listBox' .
I've corrected my code :
[java]
//Image
ImageCreator imageCreator = new ImageCreator();
imageCreator.setWidth("100%");
imageCreator.setHeight("50%");
imageCreator.setFilename( "sunthil_picture.jpg" );
imageCreator.setImageMode( "normal" );
imageCreator.setAlign( "center" );
Element imgElement = imageCreator.create(nifty, screen, listBoxDataParent);
listBoxControl.addElement( imgElement );
[/java]
But something is still missing.
( imgElement is not null but listBoxControl.addElement( imgElement ) fails )


Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: error: null
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: sun.reflect.NativeMethodAccessorImpl:null:invoke0:-2
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: sun.reflect.NativeMethodAccessorImpl:null:invoke:-1
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: sun.reflect.DelegatingMethodAccessorImpl:null:invoke:-1
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: java.lang.reflect.Method:null:invoke:-1
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: de.lessvoid.nifty.NiftyMethodInvoker:NiftyMethodInvoker.java:callMethod:164
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: de.lessvoid.nifty.NiftyMethodInvoker:NiftyMethodInvoker.java:performInvoke:115
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke:Nifty.java:perform:949
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: de.lessvoid.nifty.Nifty:Nifty.java:invokeMethods:927
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: de.lessvoid.nifty.Nifty:Nifty.java:handleDynamicElements:213
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: de.lessvoid.nifty.Nifty:Nifty.java:processMouseEvent:195
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.niftygui.InputSystemJme:InputSystemJme.java:forwardEvents:117
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: de.lessvoid.nifty.Nifty:Nifty.java:render:163
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.niftygui.NiftyJmeDisplay:NiftyJmeDisplay.java:postQueue:134
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.renderer.RenderManager:RenderManager.java:renderViewPort:664
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.renderer.RenderManager:RenderManager.java:render:691
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.app.SimpleApplication:SimpleApplication.java:update:216
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay:LwjglAbstractDisplay.java:runLoop:144
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.system.lwjgl.LwjglDisplay:LwjglDisplay.java:runLoop:133
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay:LwjglAbstractDisplay.java:run:198
Nov 26, 2010 3:03:40 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: java.lang.Thread:null:run:-1

If you change the selection (ListBoxControl.changeSelection(newIndex)) of the ListBox then the selected element should be selected and scrolled into the view. There is currently no (easy) way to show unselected items.



Adding other Elements, like images or panels to the Listbox was a planned feature (and might work) but it’s not really supported (and probably does not work) Sorry :slight_smile:

No worries.

I will manage with that for now



I will perfect it later :slight_smile: