Title: Observing complex data structures in real time in JavaFX
Tags: java;javafx;javafx-8
Question: Im trying to make a GUI layer to monitor the state in an underlying application. The data Im monitoring is sort of a trading application with the following data structure representing different markets represented and a stript down JSON version for readability.

```{
  markets: [{id: 1, runners:[{id:a, prices: [{price:10, level: 1}...]}...]},
            {id: 2, runners:[{id:a, prices: [{price:10, level: 1}...]}...]},
            {id: 3, runners:[{id:a, prices: [{price:10, level: 1}...]}...]}]
}
```

What makes this hard for me is the following:
I represent ```markets``` as an ```ObservableMap``` and display in a ```ListView``` listing all the ids for different markets. Whenever ```markets``` get updated the ```ListView```. However the tricky part is given some market id gets selected in the ```ListView``` I want to display all the runners and tier prices for the selected market and update the prices seen as they gets updated in the background. What I dont understand is how this is done in JavaFX.

```
  My question is if there are any examples online like this where you
  first see a list of things and then make a selection and only see the
  updates for this selection or could someone make suggestions how this
  use case is handled in JavaFX since it feels to be a standard case.
```

Comment: `yourListView.getSelectionModel().getSelectedItems()` returns a ObservableList. You can observe the selection using a `ListChangeListener`.

Comment: This is a rather broad question. Can you break this down and ask a specific question for aspects you are stuck with, one at a time? Are you able to write some code that simply monitors a single market? Once you have that, are you able to write code that displays the result of monitoring that in real time? Can you then add code that enables you to change the market being monitored, and then tie that to the selection in the list view? Each of these is basically a completely different aspect of what you are asking.

Here is the accepted answer: As I said in the comment, you can observe the selection of your ListView. A short example how you do that:

```public class Test
{
    public Test()
    {
        ListView<Object&gt; myListView = new ListView<&gt;();
        myListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        myListView.getSelectionModel().getSelectedItems().addListener(this181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16onSelectionChange);
    }

    private void onSelectionChange(ListChangeListener.Change<? extends Object&gt; change)
    {
        // Use the complete List to Update everything you need:
        List<? extends Object&gt; selection = change.getList(); // List of all selected Items

        // OR  use the Change to only update the Things that have been changed:
        while (change.next())
        {
            if (change.wasPermutated())
            {
                for (int i = change.getFrom(); i < change.getTo(); i++)
                {
                    //permutate
                }
            }
            else if (change.wasUpdated())
            {
                //update item
            }
            else
            {
                for (Object removedItem : change.getRemoved())
                {
                    // perform remove action
                }

                for (Object addedItem : change.getAddedSubList())
                {
                    // perform add action
                }
            }
        }
    }
}
```

In my Example the Items in the ListView are just Objects. You can change that to whatever Type you're using.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:06/15/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><javafx><javafx-8>', 'Title': 'Observing complex data structures in real time in JavaFX', 'ViewCount': 213, 'id': 44572972, 'user_AboutMe': None, 'user_AccountId': 3779712.0, 'user_DisplayName': 'user3139545', 'user_Reputation': 6290.0, 'user_Views': 532.0, 'user_id': 3139545.0}}