Nifty GUI: ListBox in NiftyGuiDemo

hello,

I changed the niftyGuiDemo available to download.

specifically I changed the LisBoxDialogController and ListBoxDialogControlDefinition.



now it is:



[java] panel(new PanelBuilder() {{

childLayoutHorizontal();

control(builders.createLabel("ListBox:"));

control(new ListBoxBuilder("listBox") {{

displayItems(4);

selectionModeSingle();

showVerticalScrollbar();

showHorizontalScrollbar();

width("300px");

control(new ControlBuilder("customListBox_Line"){{

controller("de.lessvoid.nifty.controls.listbox.ListBoxItemController");

}});

}});

}});[/java]





[java]this.listBox = getListBox2("listBox");

listBox.setListBoxViewConverter(new MessagesViewConverter());

listBox.addItem(new ListBoxMessages(nifty, "test 1", "Transport"));

listBox.addItem(new ListBoxMessages(nifty, "test 2", "Operation"));

listBox.addItem(new ListBoxMessages(nifty, "test 3", "Purchase"));[/java]



[java]@SuppressWarnings("unchecked")

private ListBox<ListBoxMessages> getListBox2(final String name) {

return (ListBox<ListBoxMessages>) screen.findNiftyControl(name, ListBox.class);

}[/java]





more classes:



[java]public ListBoxMessages(final Nifty nifty, final String message, final String typeActivity) {

this.message = message;

this.iconTransport = nifty.createImage("Interface/Activities/chatIconTransport.png", false);

this.iconStore = nifty.createImage("Interface/Activities/chatIconStore.png", false);

this.iconPurchase = nifty.createImage("Interface/Activities/chatIconPurchase.png", false);

this.iconOperation = nifty.createImage("Interface/Activities/chatIconOperation.png", false);

this.iconOperator = nifty.createImage("Interface/Activities/iconOperator.png", false);

this.iconShip = nifty.createImage("Interface/Activities/chatIconShip.png", false);

if (typeActivity.equals("Transport"))

iconSelected = iconTransport;

else

if (typeActivity.equals("Store"))

iconSelected = iconStore;

else

if (typeActivity.equals("Purchase"))

iconSelected = iconPurchase;

else

if (typeActivity.equals("Operation"))

iconSelected = iconOperation;

else

if (typeActivity.equals("Ship"))

iconSelected = iconShip;

else

iconSelected = iconOperator;

}



public String getText() {

return message;

}



public NiftyImage getIcon(){

return iconSelected;

}[/java]





[java]public class MessagesViewConverter implements ListBoxViewConverter<ListBoxMessages> {

private static final String CHAT_LINE_ICON = "#icon";

private static final String CHAT_LINE_TEXT = "#message";



/**

  • Default constructor.

    */

    public MessagesViewConverter() {

    }



    /**
  • {@inheritDoc}

    */

    @Override

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

    final Element text = listBoxItem.findElementByName(CHAT_LINE_TEXT);

    if (text == null) return;

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

    final Element icon = listBoxItem.findElementByName(CHAT_LINE_ICON);

    final ImageRenderer iconRenderer = icon.getRenderer(ImageRenderer.class);

    if (item != null) {

    textRenderer.setText(item.getText());

    iconRenderer.setImage(item.getIcon());

    } else {

    textRenderer.setText("");

    iconRenderer.setImage(null);

    }

    }



    /**
  • {@inheritDoc}

    */

    @Override

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

    final Element text = listBoxItem.findElementByName(CHAT_LINE_TEXT);

    if (text == null) return 0;

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

    final Element icon = listBoxItem.findElementByName(CHAT_LINE_ICON);

    final ImageRenderer iconRenderer = icon.getRenderer(ImageRenderer.class);

    return ((textRenderer.getFont() == null) ? 0 : textRenderer.getFont().getWidth(item.getText()))
  • ((item.getIcon() == null) ? 0 : item.getIcon().getWidth());

    }

    }

    [/java]



    and the result is the image attached.

    http://imageshack.us/f/600/niftyerror.png/

    but the problem is:

    ‘‘I cannot select any item’’

    and the question is ‘‘why?’’

    please help about that.

no double-posting:

http://hub.jmonkeyengine.org/groups/gui/forum/topic/nifty-gui-listbox-with-a-controlbuilder-textfield-problem/#post-180524