[Solved] ListBox not accepting selection (3 posts)

Topic tags: gui, jME3, listbox, nifty, nifty-gui
  • Profile picture of Tumaini Tumaini79p said 3 months, 4 weeks ago:

    I’m having trouble with a listBox control, being unable to select any items in it – no visual cue, no callback, just the list of items displayed.

    Here’s my xml:

    <control name="listBox" id="groups_list" width="100%" height="*"
    	vertical="on" horizontal="off" displayItems="15" selection="Single"
    	viewConverterClass="com.limewoodGames.holicity.client.gui.community.GroupViewConverter">
    	<control name="group-list-item"/>
    </control>

    The group-list-item definition:

    <controlDefinition name="group-list-item">
    	<panel childLayout="horizontal" width="100%" align="center">
    		<image id="#group-icon" width="20px" height="20px" />
    		<control name="label" id="#group-name" font="Interface/Fonts/freesans-12.fnt" align="left"
    			textHAlign="left" wrap="true" height="20px" width="*" />
    	</panel>
    </controlDefinition>

    The GroupViewConverter:

    public class GroupViewConverter implements ListBoxViewConverter<CGroup> {
    	private static final String GROUP_ICON = "#group-icon";
        private static final String GROUP_NAME = "#group-name";
    
        /**
         * Default constructor.
         */
        public GroupViewConverter() {
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public final void display(final Element listBoxItem, final CGroup item) {
            final Element text = listBoxItem.findElementByName(GROUP_NAME);
            final TextRenderer textRenderer = text.getRenderer(TextRenderer.class);
            final Element icon = listBoxItem.findElementByName(GROUP_ICON);
            final ImageRenderer iconRenderer = icon.getRenderer(ImageRenderer.class);
            if (item != null) {
                textRenderer.setText(item.getName());
                iconRenderer.setImage(item.getIcon());
            } else {
                textRenderer.setText("");
                iconRenderer.setImage(null);
            }
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public final int getWidth(final Element listBoxItem, final CGroup item) {
            final Element text = listBoxItem.findElementByName(GROUP_NAME);
            final TextRenderer textRenderer = text.getRenderer(TextRenderer.class);
            return ((textRenderer.getFont() == null) ? 0 : textRenderer.getFont().getWidth(item.getName()))
                    + ((item.getIcon() == null) ? 0 : item.getIcon().getWidth());
        }
    }

    The list displays great and you can add items without a problem, but there’s no selection at all.
    Do you have to do something else to enable selection in a listBox or did I use it in an incorrect way?

  • Profile picture of Tumaini Tumaini79p said 3 months, 4 weeks ago:

    Update:
    I added the nifty-listbox-item style and noticed I could use the arrow keys (up/down) and space bar to select items.
    So it’s the mouse interaction that’s not working, not even with visibleToMouse=”true” on all elements and controls.
    I can’t use the listbox.selectItemByIndex(int i) either, it doesn’t select any items.

  • Profile picture of Tumaini Tumaini79p said 3 months, 4 weeks ago:

    I found the solution!
    You have to set your custom control’s controller to de.lessvoid.nifty.controls.listbox.ListBoxItemController as it isn’t done automatically.

    Here’s an example:

    <panel childLayout="vertical" width="100%" height="100%">
    	<control name="listBox" id="groups_list" width="100%" height="100%" vertical="optional" horizontal="off" displayItems="15"
    		viewConverterClass="com.limewoodGames.holicity.client.gui.community.GroupViewConverter">
    		<control name="group-list-item" controller="de.lessvoid.nifty.controls.listbox.ListBoxItemController" />
    	</control>
    </panel>