JavaFX Application - architectural issue - diffrent lists of inherited classes

136 Views Asked by At

I'm learning JavaFX with this tutorial http://code.makery.ch/library/javafx-8-tutorial/, but in my app i've made login module and want to create three diffrent views - depending on the type of user: Admin, Patient, Doctor, and all of them inherit from User class. For now I create Doctor view and keep list of patients in Main class:

private ObservableList<Patient> patientData = FXCollections
            .observableArrayList();

    public ObservableList<Patient> getPatientData() {
        return patientData;
    }

    public Main() {
        patientData.add(new Patient("Hans", "Muster", 23));
    } 

I dont know what should my next move be. If I make one list with User and keep them Doctors, Patients and Admins it will be a problem for generating proper views, cause e.g Doctor have only Patients' List. Another big problem for me is how to serialize it to XML. If I make three separate list it won't be 'elegant' way I think.

1

There are 1 best solutions below

11
On

You could create a single ObservableList<User>, and then populate the various views using a FilteredList.

E.g.

ObservableList<User> userList = FXCollections.observableArrayList();

// ...

ListView<User> patientView = new ListView<>();
patientView.setItems(new FilteredList<>(userList, Patient.class::isInstance));

If you want a ListView<Patient> instead of a ListView<User>, the easiest way is to use the EasyBind library:

ListView<Patient> patientView = new List<View>();
patientView.setItems(EasyBind.map(new FilteredList<>(userList, Patient.class::isInstance), 
    Patient.class::cast);