1

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.

Tom
  • 43
  • 7
  • Why can't you make one list of Users and just have a test to see if type is doctor before showing patient list? – River Jun 13 '15 at 17:38
  • If I had done that way, I would need to check every object in the list, cause it would be one list - with doctors, patients, admins. And I have no idea how to serialize it. – Tom Jun 13 '15 at 17:47
  • Don't you only have to check the object that's logging in? – River Jun 13 '15 at 17:50
  • I load list of users on the beggining, than I log in. If e.g. I'm a doctor I want to see list of all patients - only. But all patients are on the list of Users, as well as admins, doctors. – Tom Jun 13 '15 at 17:58
  • I mean you could create a separate pure patients list. That would only use two lists. I'll try to think of a better way though. – River Jun 13 '15 at 18:06
  • I have it done this way, and proably get your point - admin can see only doctors list so it's same situation. – Tom Jun 13 '15 at 18:12

1 Answers1

0

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);
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Ok, but do you know how JAXB will handle with this? When I tried save User list, it only saved User objects (what was easy to predict), without information from inehrtitated classes. – Tom Jun 13 '15 at 19:57
  • No; I don't know JAXB. But that surprises me; it doesn't use reflection to get the runtime type of each object and serialize the properties? – James_D Jun 13 '15 at 19:58
  • Well, no, unless I was doing something stupid. I have wrapper class with list of users and getter to this list with XML Annotation. – Tom Jun 13 '15 at 20:04
  • Does http://stackoverflow.com/questions/4144296/marshalling-a-list-of-objects-with-jaxb help? Again, with JAXB I am somewhat out of my depth. – James_D Jun 13 '15 at 22:42
  • Sorry for responding so late, it seems works. I have a problem with EasyBinding - I download jar from here: [link](https://github.com/TomasMikula/EasyBind) but I don't have there org.fxmisc.easybind.EasyBind class – Tom Jun 15 '15 at 23:14
  • The class is there... not sure why you are having problems. – James_D Jun 15 '15 at 23:18
  • I've got only org.fxmisc.easybind.monadic and org.fxmisc.easybind.select – Tom Jun 15 '15 at 23:53
  • Those are the only subpackages but there should be a bunch of classes too. If you list the contents of the jar file you should see about 40 classes or so, including `org.fxmisc.easybind.EasyBind`. If it's not there you must have somehow got a corrupt jar file. – James_D Jun 16 '15 at 00:05
  • Ok, I clear it & download again and have it all. However, with this solution I have problems with removing (patientsTable.getItems() to be specific) and after loading XML file I don't have Patients on the tableview (though JAXB loads it correctly from a file) – Tom Jun 16 '15 at 00:39
  • Manipulate the items in the main list, not the filtered list. – James_D Jun 16 '15 at 00:40
  • Yes, I'd doing this only at the begining: ObservableList data = mainApp.getPatientData(); patientsTable.setItems(EasyBind.map(new FilteredList<>(data, Patient.class::isInstance), Patient.class::cast)); Removing or loading I'm doing on the User list in Main class. – Tom Jun 16 '15 at 00:53