Custom listbox using java

I’m trying to build a very simple custom listbox but I’m having a hard time converting the xml from the wiki post to java.

Here’s the code where I’m trying to define the listbox:

[java]

panel(new PanelBuilder(“Squad1”) {



{

height(“100%”);

width(“200px”);

childLayoutHorizontal();

control(new ListBoxBuilder(“SquadOneList”) {



{

hideVerticalScrollbar();

hideHorizontalScrollbar();

selectionModeDisabled();

height(“100%”);

width(“100%”);

viewConverterClass(SquadListViewConverter.class);

control(new ControlBuilder(“SquadItem”) {



{

childLayoutHorizontal();

control(new LabelBuilder(“SquadListPlayerName”, “”));

}

});



}

});

}

});

[/java]

And this is the view converter:

[java]

public class SquadListViewConverter implements ListBoxViewConverter<ListBoxPlayer> {

private static final String labelID = “SquadListPlayerName”;

public SquadListViewConverter() {

}

@Override

public final void display(final Element listBoxItem, final ListBoxPlayer item) {

final Element text = listBoxItem.findElementByName(labelID);

final TextRenderer textRenderer = text.getRenderer(TextRenderer.class);

if (item != null) {

textRenderer.setText(item.toString());

} else {

textRenderer.setText("");

}

}

@Override

public final int getWidth(final Element listBoxItem, final ListBoxPlayer item) {

final Element text = listBoxItem.findElementByName(labelID);

final TextRenderer textRenderer = text.getRenderer(TextRenderer.class);

return ((textRenderer.getFont() == null) ? 0 : textRenderer.getFont().getWidth(item.toString()));

}

}

[/java]

The code compiles, however a nullPointerException is being raised on this line:

[java]

final TextRenderer textRenderer = text.getRenderer(TextRenderer.class);

[/java]

because it’s unable to find my label element.

I’m assuming the problem is that I’m incorrectly defining the custom listbox item?

Still haven’t resolved this. Any input would be greatly appreciated.

Well, the problem is, that the ListBox expects another control as the actual list box item. Unfortunatly you can’t directly build this control on the fly, like you’ve tried with the LabelBuilder(“SquadListPlayerName”, “”) call.



I’ve posted a complete example on how to do that here http://sourceforge.net/projects/nifty-gui/forums/forum/807893/topic/4659629. That was a question on how to use a custom ListoBox to kinda “simulate” a table control.



Everything else looks pretty ok in your example.

Thanks. I’ll take a look at that post.