ListBox selection notification

Hi !

I’m just trying to code a quick tool using ListBoxes, but I’m a little bit confused about how the GUI gets notified when the user selects another item in the List.

Basically my (very simple) app is divided in a Model and a View/Controler (a basic MVC). I have a ListBox in the view, and a ListBoxModel in the Model.
If I change the selection in the ListBox, the ListBoxModel is notified inside the overrided selectedRowsChanged . However, how can I make the other GUI classes aware of a change of selection ?
In MVC, the model (ListBoxModel in that case) is not supposed to be aware of any GUI class at all, so I can’t call a method from the GUI from ListBoxModel.

I couldn’t find any method in ListBox, that I can override to be notified when the selection changed, and looking at the code, it doesn’t seem to exist any.

What’s the recomended way of doing such things ?

Allowing multiple listeners to directly listen to a listbox is probably bad design… E.g. if your list is selecting a type of cheese, you’d have an object somewhere of type CheeseManager, and the listboxmodel would call myCheeseManager.setCurrentCheese() when the selection changes. Then, other CheeseListener objects would attach themselves to CheeseManager, and receive callbacks when the cheese changes, which is a far better design than having them directly connected to a listbox.

[quote=“jules”] if your list is selecting a type of cheese[/quote] Haha, do you say that because I’m french ? :slight_smile:

Ok thanks for this input, I can totally see your logic there and it makes sense to me. Thanks !

No, I didn’t notice that you were french! But cheese is a useful substance for computing analogies!

1 Like