gridviewjavafxselectionmodelcontrolsfx

Proper way of Implementing JavaFX' SelectionModel to a View that doesn't have it


I'm attempting to implement a MultipleSelectionModel for ControlsFX' GridView. Researching the internet, Jonathan Giles mentioned that it was a bad idea to add listeners to every GridCell. This left me wondering how to notify the selection model of events such selection, deselection etc. Before reading that, I was thinking that the Node used in the GridCell will consume a mouse/touch/key event and use the GridCells' updateSelected method accordingly. My question here is if this updating propagates to any listeners/models at all, or do I have to bind it (somewhere) manually, which I don't know how to do at the moment.

Also, selection models use a read-only observable list, which poses another issue as to how to bind the GridView's data (i.e. items list) to the selection model's list, which I assume is what the model uses for its processes.

I could probably bypass using a selection model altogether, and go with intercepting input events at the Cell/Node level, and do stuff like highlighting on the Cell's updateItem method, while using a backing ObservableList or 2, to track everything, and refresh accordingly, but this isn't the best way, much less a good one. A selection model for GridView, should be the only way to go, which is why I'm here.


Solution

  • You don't necessarily have to use a MultiSelectionModel or whatever JavaFX brings along. It's sufficient to use a Set or a List to store the nodes you select.

    Here's an example about How to select multiple components of Canvas using ctrl key in javafx? with an answer from me. I guess it covers your needs. If not, please be specific about what you need.