[Solved] Lemur ListBox clicks not registering

Guys… remember that the selection model is a versioned object.

VersionedReference<Set<Integer>> selectionRef = listBox.getSelectModel().createReference();
...
...
public void update( float tpf ) {
    if( selectionRef.update() ) {
        // Do something because the selection changed... like if you put Chassis objects in your list box...
        Chassis selected = listBox.getModel().get(listBox.getSelectionModel().getSelection());
    }
}
}

…or whatever.

1 Like

Uh… don’t use a list box. Since that’s not really what you want anymore.

Once you start trying to take over all of the stuff the list box is already doing for you then you might as well not use it anymore.

Edit: I mean I guess it’s supposed to work. Looking at the ListBox code it’s calling the list box click listeners whenever items receive click events. But a click listener per item is like the least efficient way possible to do what you want to do.

The thing is that using the selection would just keep switching parts in an infinite loop since one has to be selected. What I want is to click on one part in one listbox, then click on the other in another listbox and have them swapped.

Let me see if I can be clearer with some code snippets. Without knowing specifics about your code, I’m going to assume there are some kind of “Part” object and that is what they are selecting.

// Setting up the list
ListBox<Part> partsList = new ListBox<>(.....);
partsList.setCellRenderer(new DefaultCellRenderer<Part>() {
        protected String valueToString( Part p ) {
              // whatever to convert p to string
        }
    });

// Showing the parts you want selected
partsList.getModel().addAll(theParts);

// Keep tracking of which part is selected
VersionedReference<Set<Integer>> selectionRef = partsList.getSelectionModel().createReference();

// During update or whatever
if( selectionRef.update() ) {
    // Selection has changed
    if( selectionRef.get().isEmpty() ) {
        setSelectedPart(null);
    } else {
        setSelectedPart(partsList.getModel().get(partsList.getSelectionModel().getSelection());
    }
}

No weird back flips for in/out conversion. No strange multiple click listeners that all effectively do the same thing anyway… etc.

1 Like

I see what you mean. But the selection has the problem that one part is always selected. Since I’m swapping parts in two listboxes that would effectively create a swap-loop, rendering it unusable.

This is separate from your click problem. But if you adopt the approach provided then you would only need one click listener (on the target) anyway. Simplifies the problem greatly and eliminates one of the places where errors can be.

For example, I couldn’t see any System.out.printlns() where you verified that the click listener was being called or not. So I have to guess that it’s at least a possibility that your if statement is failing for some reason.

Edit: the click listener becomes as simple as “swap this part selection with that part selection”

1 Like

I’ve rewritten most of it now, and it’s working fine!
Thanks @pspeed for taking the time to help me.

1 Like