Updating a JavaFX ListView after service update I wrote a JavaFX program that contains a list of notes, each having a title and a content.

This is the controller code:

public class MainPresenter implements Initializable
{
    @FXML
    private ListView&lt;Note&gt; listView;

    @FXML
    private TextField titleField;
    @FXML
    public TextArea contentField;
    @FXML
    public Button saveButton;

    @Inject
    private NoteService noteService;


    @Override
    public void initialize( URL location, ResourceBundle resources )
    {
        listView.setCellFactory( param -&gt; new NoteListCell() );
        listView.setItems( FXCollections.observableArrayList( noteService.getNotes() ) );
        listView.getSelectionModel().selectedItemProperty().addListener( new NoteListViewSelectionChangeListener() );

        selectFirstItemIfPossible();

        saveButton.setOnAction( event -&gt; {

            // Save the updated note with the service
            Note note = new Note( listView.getSelectionModel().getSelectedItem().getId(),
                                  titleField.getText(),
                                  contentField.getText() );
            noteService.updateNode( note );

            // Update the view
            int selectedIndex = listView.getSelectionModel().getSelectedIndex();
            listView.setItems( FXCollections.observableArrayList( noteService.getNotes() ) );
            listView.getSelectionModel().select( selectedIndex );

        } );
    }

    private void selectFirstItemIfPossible()
    {
        if (listView.getItems().size() &gt; 0)
        {
            listView.getSelectionModel().select( 0 );
        }
    }

    private static class NoteListCell extends ListCell&lt;Note&gt;
    {
        @Override
        protected void updateItem( Note item, boolean empty )
        {
            super.updateItem( item, empty );
            if (item != null)
            {
                setText( item.getTitle() );
            }
        }
    }

    private class NoteListViewSelectionChangeListener implements ChangeListener&lt;Note&gt;
    {
        @Override
        public void changed( ObservableValue&lt;? extends Note&gt; observable, Note oldValue, Note newValue )
        {
            if( newValue != null )
            {
                titleField.setText( newValue.getTitle() );
                contentField.setText( newValue.getContent() );
            }
        }
    }
}


I am mainly looking for feedback on the code that gets run when you click on the save button:

saveButton.setOnAction( event -&gt; {

            // Save the updated note with the service
            Note note = new Note( listView.getSelectionModel().getSelectedItem().getId(),
                                  titleField.getText(),
                                  contentField.getText() );
            noteService.updateNode( note );

            // Update the view
            int selectedIndex = listView.getSelectionModel().getSelectedIndex();
            listView.setItems( FXCollections.observableArrayList( noteService.getNotes() ) );
            listView.getSelectionModel().select( selectedIndex );

        } );


I now need to re-fetch all notes from the noteService again to be able to update the list view. I guess there is a better way to do it?
Found a better way myself:

saveButton.setOnAction( event -&gt; {
            // Save the updated note with the service
            Note selectedItem = listView.getSelectionModel().getSelectedItem();
            selectedItem.setTitle( titleField.getText() );
            selectedItem.setContent( contentField.getText() );
            noteService.updateNode( selectedItem );

            // First update the item in the model
            listView.getItems().set( listView.getSelectionModel().getSelectedIndex(), selectedItem );
            // Trigger a sort of the items. Could be avoided by checking if the title really did change
            listView.getItems().sort( new NoteComparator() );


This avoids getting the full lists of notes from the service again each time.