Call function on Lemur Dropdown value change

Hello

I have been trying to look through the documentation and the Javadoc and can’t find a way to achieve this. I am using Lemur to handle my GUI, and I want to call a function after the user changes the selected value of a dropdown (specifically a Selector). Basically I want to achieve this

Selector<String> dropdown = new Selector(values);
dropdown.addOnChangeCommands(new Command<Selector>{
  @Override
  public void execute(Selector dropdown) {
     System.out.println("User selected " + dropdown.getSelectedItem());
  }
});

Is there a way to achieve this? If not would it perhaps be worth creating an issue in the Lemur github project?

Lemur works on VersionedReferences instead of events. Events tend to produce a ton of garbage and become a source of memory leaks as callers forget to remove them, they suck in the whole outer class as part of their memory profile, etc… In a system where there is a constant update() loop, watched references tend to make more sense and be more convenient than the cumbersome listener management.

So the “Lemur way” would be to create a versioned reference for the selected item:
http://jmonkeyengine-contributions.github.io/Lemur/javadoc/LemurProto/com/simsilica/lemur/Selector.html#createSelectedItemReference--

VersionedReference<String> myRef = dropdown.createSelectedItemReference();

…then watch that in the update() loop.

    if( myRef.update() ) {
        // do something with myRef.get()
    }

And quite commonly, things like:

    boolean refreshView = false;
    if( myRef.update() ) {
        refreshView = true;
    }
    if( myOtherRef.update() ) {
        refreshView = true;
    }
    ....
    if( refreshView ) {
       redrawAllTheThings();
    }

When the models could be anything and sometimes not even set from a user action (versioned references work for all kinds of stuff and they are easy to setup), then they also handily skip the event churn that can happen when an ‘thing’ updates 50 times before the UI is even ready to repaint.

4 Likes