0

It's an application that manages students, and it has a class named StudentsViewController that links the graphical part(the StudentView) and the functions that effectively make the changes on an ArrayList(the Service). I'll show you the part I don't understand in JavaFX Graphic User Interfaces:

public class StudentViewController implements Observer<Student>{
    private ObservableList<Student> model;
    private StudentView view;
    StudentService service;

    public StudentViewController(StudentService service, StudentView view){
        this.view=view;
        this.model= FXCollections.observableArrayList(service.getAllStudents());
        view.studTable.setItems(model);
        this.service=service;
     }

    @Override
    public void update(Observable<Student> observable) {
        StudentService s=(StudentService)observable;
        model.setAll(s.getAllStudents());
    }

}

My question is:

If I have an ObservableList that wraps my ArrayList and a TableView that uses the ObservableList, why do I need the update function?

Why do I have to clear all the data from my model and put a new one there?

Corina
  • 15
  • 7
  • JavaFX doesn't even have an `Observer` class. And it is difficult to say what is trying to be accomplished here without further info on what the purpose and goal is. Even with further info, I suspect this will be a deceptively difficult question to answer well. – jewelsea Dec 02 '16 at 01:51
  • For a nice introduction to JavaFX design (which includes a view of a table of person data backed by a model similar to your goal), I recommend reviewing the [makery JavaFX tutorial](http://code.makery.ch/library/javafx-8-tutorial/). In trying to answer this question on how this "should be done" I found I was just emulating the essence of programming patterns used in the tutorial anyway. – jewelsea Dec 02 '16 at 01:52
  • To better understand the use of observers in GUI architectures in general see: [GUI Architectures by Martin Fowler](http://martinfowler.com/eaaDev/uiArchs.html). Note that JavaFX has a whole host of inbuilt controls and classes for dealing with observable properties, so those are often used directly rather than implementing your own `Observer` style code as is done in some of the GUI Architectures examples and in the code in your sample question. – jewelsea Dec 02 '16 at 01:59
  • For related information on why an ObservableList without extractors may be insufficient for monitoring changes to items within the list, see the info on [callbacks and extractors](http://stackoverflow.com/questions/31687642/callback-and-extractors-for-javafx-observablelist). The lists you deal with in your example do not define extractors, so that may be a reason for the `update` function and the `setAll` call within it. Though, my guess is that the author is trying to separate JavaFX properties from model objects and services (not always necessary), so that could be another reason. – jewelsea Dec 02 '16 at 02:04

1 Answers1

0

After more research I found the answer:

public static <E> ObservableList<E> observableList(List<E> list)

Constructs an ObservableList that is backed by the specified list. Mutation operations on the ObservableList instance will be reported to observers that have registered on that instance. Note that mutation operations made directly to the underlying list are not reported to observers of any ObservableList that wraps it.

So this means that if I make changes on the list of students (which is the underlying list), the table view won't be notified(which is the observer), so it won't refresh itself. This is why I need the update function, to wrap the new list into the ObservableList, operation that will notify the observer.

Corina
  • 15
  • 7