Listbox not able to show text and other problems

Hi, I’m trying to make a chat area with a listbox, but I’m having a few problems. first, when displaying the listbox the area where the text should be isn’t shown. Here is my xml:



[xml] <layer id=“layer” childLayout=“vertical”>

<panel id=“main_panel” style=“nifty-panel” childLayout=“vertical” width=“40%” height=“30%” align=“left” valign=“center”>

<control id=“chatArea” name=“listBox” vertical=“optional” horizontal=“false” width=“100%” height=“100%” displayItems=“4” selection=“Disabled” visibleToMouse=“true” viewConverterClass=“gui.ListBoxView”>

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

</panel>

</control>

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

<control name=“textfield” id=“textMessage” width=“65%” />

<control name=“button” id=“sendChat” label=“Send”>

<interact onClick=“sendMessage()” />

</control>

</panel>

</panel>

</layer>[/xml]



I’m also getting these warnings:

de.lessvoid.nifty.controls.listbox.ListBoxControl createViewConverter

WARNING: Unable to instantiate given class [gui.ListBoxView] with error: gui.ListBoxView

java.lang.InstantiationException: gui.ListBoxView



WARNING: you’re using the ListBoxViewConverterSimple but there is no TextRenderer on the listBoxElement.You’ve probably changed the item template but did not provided your own ListBoxViewConverter to the ListBox.



here is the listboxview class:

[java]public class ListBoxView implements ListBoxViewConverter {

private String text;



public ListBoxView(final String text){

this.text = text;

}



public String toString(){

return text;

}



public void display(Element elmnt, Object t) {

//throw new UnsupportedOperationException(“Not supported yet.”);

}



public int getWidth(Element elmnt, Object t) {

//throw new UnsupportedOperationException(“Not supported yet.”);

return 10; //Don’t know what to do here actually;

}

}[/java]



this is how I add text: (I think this works but I don’t know for certain)

[java]

private static ListBox<ListBoxView> chatArea;

chatArea = (ListBox<ListBoxView>) nifty.getScreen(“Chat”).findNiftyControl(“chatArea”, ListBox.class);

chatArea.addItem(new ListBoxView(text));

[/java]



I hope somebody can make anything from this, sorry if it’s a little messy, but I don’t know where to look anymore :s

The ListBoxViewConverter is what handle the item you insert in your list. Not the items themselves.



The way you define your viewConverter is correct :

[java]

<control id=“chatArea” name=“listBox” vertical=“optional” horizontal=“false” width=“100%” height=“100%” displayItems=“4” selection=“Disabled” visibleToMouse=“true” viewConverterClass=“gui.ListBoxView”>

[/java]



The issue is with the converter itself.

What type of object do you want to handle ? String, custom classes ?



If you want to handle String, you don’t need a ListBoxViewConverter. The default one handle them correctly.

If you want to handle custom classes, you need to define your own ListBoxViewController.

Here is an example with a class named CharacterItem :

ListBoxViewConverter

[java]

public class ListBoxViewConverterForItems implements ListBoxViewConverter<CharacterItem> {



@Override

public void display(final Element element, final CharacterItem item)

{

ItemButtonControl control = element.getControl( ItemButtonControl.class );

control.setItem( item );

}



@Override

public int getWidth(final Element element, final CharacterItem item)

{

return 300;

}

}

[/java]

The important method is the first one :

The argumenet element refers to the line that will be added to the list. This is where you define what need to be displayed.



XML

[java]

<control id=“miscList” name=“ae-listBox” height="*" width=“100%” vertical=“true” horizontal=“optional” displayItems=“10” selection=“Single” viewConverterClass=“com.didigame.acariaempire.gui.controls.ListBoxViewConverterForItems” >

<control name=“itemButton” />

</control>

[/java]

The first element defined insed the list is used as template. In this list, all lines will be controls ( itemButton ).

Note : I think that you need to define the height of the line in this list.

( In my case, the controlDefinition of the itemButton control has static size : ( 25 , 300 ) )



Populate the list

[java]

ListBox<CharacterItem> listBox = miscList.getNiftyControl( ListBox.class );

listBox.clear();



Equipment eqt = character.getEquipment();

for( CharacterItem item : eqt.getBagItems() )

listBox.addItem( item );

[/java]

What you insert in the list are not ListBoxViewConverter but CharacterItem.



If you want to add more than one line from a String, you need to do it before adding them to listBox.
Something like :
[java]
String lines[] = text.split(...);
ListBox<String> listBox = miscList.getNiftyControl( ListBox.class );
for( int i = 0; i < lines.length; i++ )
listBox.addItem( lines );
[/java]