Nifty ListBox onclick/selection event on listItem

Hello,
I have a ListBox in my GUI. I want that if you click on a listItem, an event gets activated.
I have googled but I don’t really understand how to do this.
It is important to know which listItem is clicked on. I also want to have a parameter passed to the event method like a number.

This is how I add my items to the listbox:

ListBox listBox = screen.findNiftyControl("user-list", ListBox.class); listBox.addItem("User 1"); // If this listItem becomes selected, the parameter 1 should be passed to the event method, like: listItemSelected(1); listBox.addItem("User 2"); // Pass parameter 2 to event method. Like: listItemSelected(2); // ...

Add a listener to your controller that listens for “OnSelected” in your listbox, then the callback will tell you which item is clicked. Something likes this:

@NiftyEventSubscriber(id="listBox")
  public void onListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent changed) {
      
      //your code here
  }