用ArrayList填充TableView

时间:2019-05-16 16:48:27

标签: javafx arraylist tableview

我有一个包含数据的ArrayList,我想在我的tableview中显示此数据

验证我的ConcreteObservable类具有正确的参数名称。

public class DisplayPatients implements Initializable {
    @FXML
    private Button btnMonitorPatients;
    @FXML
    private TableView<ConcreteObservable> allPatientsTable;
    private String firstName, lastName, dob, phoneNumber, cholesterolLevel, gender, id;
    public static List<ConcreteObservable> PATIENTSLIST;
    private ConcreteObservable myPatient;
    Patient patient;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        PATIENTSLIST = new ArrayList<>();
        try {

              //retrieve patientdata and store it in List, this bit works just fine

                PATIENTSLIST.add(this.myPatient);
            });
        } catch (Exception e) {
            System.out.println("An error occurred trying to search:");
            e.printStackTrace();
        }
        TableColumn<ConcreteObservable, String> firstNameColumn = new TableColumn<>("Name");
        firstNameColumn.setMinWidth(200);
        firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        TableColumn<ConcreteObservable, String> lastNameColumn = new TableColumn<>("Surname");
        lastNameColumn.setMinWidth(200);
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        TableColumn<ConcreteObservable, String> dobColumn = new TableColumn<>("Year of Birth");
        dobColumn.setMinWidth(200);
        dobColumn.setCellValueFactory(new PropertyValueFactory<>("dob"));
        TableColumn<ConcreteObservable, String> phoneNumberColumn = new TableColumn<>("Contact");
        phoneNumberColumn.setMinWidth(200);
        phoneNumberColumn.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
        TableColumn<ConcreteObservable, String> genderColumn = new TableColumn<>("Gender");
        genderColumn.setMinWidth(200);
        genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
        TableColumn<ConcreteObservable, String> idColumn = new TableColumn<>("ID");
        idColumn.setMinWidth(200);
        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        allPatientsTable = new TableView<>();
        allPatientsTable.setItems(getPatients());
        //noinspection unchecked
        allPatientsTable.getColumns().addAll(firstNameColumn,lastNameColumn,dobColumn,phoneNumberColumn,genderColumn,idColumn);


    }

    public ObservableList<ConcreteObservable> getPatients() {
        return FXCollections.observableArrayList(PATIENTSLIST);
    }


}

现在,显示的表格视图为空,我已使用此页面Casting ArrayList<> to ObservableList<> for TableView在网站上搜索了类似的问题

但是我仍然停留在显示数据上。我在哪里迷路?

这是ConcreteObserver类

package observerimplementation;

import observerpattern.IObservable;
import observerpattern.IObserver;


import java.util.ArrayList;
import java.util.List;

public class ConcreteObservable implements IObservable {
    List<IObserver> practitioner = new ArrayList<>();
    private String firstName, lastName, dob, phoneNumber, cholesterolLevel, gender, id;

    public ConcreteObservable(){

    }

    @Override
    public void addObserver(IObserver practitioner) {
        this.practitioner.add(practitioner);
    }

    @Override
    public void removeObserver(IObserver practitioner) {
        this.practitioner.remove(practitioner);
    }

    @Override
    public void notifyObserver() {
        practitioner.forEach(IObserver::update);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void getPatients() {


    }


}

0 个答案:

没有答案