Accessing a Listbox's Scrollbar

Hello, I’m fairly new to working with Nifty GUI, and I am trying to make a simple chat client. It was actually not that hard to set it all up, but I’ve run in to an odd problem that I haven’t seen mentioned anywhere. The scrollbar for the listbox isn’t updating when I add new items to it! It remains at the maximum size, making it impossible to scroll downward to see newer messages. I imagine this would make a rather poor chat client as it is now, since the user would only be able to view 3-4 lines of text!



I thought that the listbox would automatically update the scrollbar as I added items to it. I’m guessing that it either doesn’t automatically update the scrollbar, or it isn’t recognizing that I’ve placed items there. I’ve checked for a means of updating it manually, but haven’t come up with much. Would anyone happen to have any suggestions?



XML Code:

[xml] <!-- Default HUD -->

<screen id=“default_hud” controller=“mygame.ChatClientController”>

<layer id=“layer” childLayout=“vertical”>

<panel id=“top_panel” height=“75%” width=“100%”/>

<panel id=“bottom_panel” childLayout=“horizontal” height=“25%” width=“50%” align=“left”>

<panel id=“chat_panel” childLayout=“vertical”>

<control id=“chatList” name=“listBox” vertical=“true” horizontal=“false” height="*" childLayout=“vertical” visibleToMouse=“true” displayItems=“8”>

<!-- Note: Child panel MUST be called “chatListData”! -->

<panel id=“chatListData” childLayout=“vertical” visibleToMouse=“true” width=“100%” padding=“20px”>

<!-- Test Label:

<label text=“Default” color="#f00f" style=“nifty-listbox-item” />/>

–>

</panel>

</control>

<panel id=“chat_bottom_bar” childLayout=“horizontal”>

<control id=“chat_text” name=“textfield” valign=“center”>

<interact onEnter=“sendChat()”/>

</control>

<control id=“send_button” name=“button” label=“send” valign=“center”>

<interact onClick=“sendChat()”/>

</control>

</panel>

</panel>

</panel>

</layer>

</screen>[/xml]



I set it up in Java as follows:

[java]

ListBoxControl lBox;



public void bind(Nifty nifty, Screen screen)

{

this.nifty = nifty;

this.screen = screen;

lBox = screen.findControl(“chatList”, ListBoxControl.class);

lCount = 0;



}



addChat(ChatMessage cM)

{



lBox.addItem(c.msg); //Add message to list

lCount++; //Increment list item counter

lBox.changeSelection(lCount); //Select final item

//in list.



}

[/java]



Any assistance on the matter would be appreciated. :slight_smile:

I found a fix for this somewhere, you have to use a custom style for listbox elements:

[xml]

<style id="my-listbox-item-style" base="nifty-listbox-item">

<effect>

<onCustom name="updateScrollpanelPositionToDisplayElement" target="listBoxStatic" oneShot="true" />

</effect>

</style>

[/xml]

Wow! That was a really quick reply. Sorry for taking so long to respond in my own thread!



How exactly would one make use of that in a dynamic sense, though? For example, I can use a LabelCreator, and then set the style to “my-listbox-item-style”, but then things become a little confusing. In order for the style to do anything, I’d need to call onCustom() for the label that was added to the list, right? The problem is, I can’t seem to find any way of directly interacting with the labels in order to do that! Perhaps I’m missing something, or worse, doing this horribly wrong to begin with?



I actually (sort of) found a fix myself. It occurred to me that when items for the listbox were statically defined, the scrollbar seemed to modify itself to accommodate the number of static items. I got the idea that, by default, the listbox will only update the scrollbar at initialization. It probably wasn’t meant to be used this way, and I’m not sure if this might result in strange behavior over time, but I decided to try calling my ListBoxControl’s onStartScreen() method immediately after adding a new item to the list. I was surprised to find that it actually caused the scrollbar to update!



Of course, I probably shouldn’t be doing it that way. If you wouldn’t mind, could you provide a simple example of how that effect could be invoked dynamically?