Java ObservableList不显示某些值

时间:2019-03-02 23:19:01

标签: java javafx arraylist netbeans observablelist

我正在创建一个程序,允许房东使用给定的信息注册新的租户。该程序可以完美地写入和读取文件,但是我遇到的问题是,使用ObservableList填充TableView无法处理所有数据。租户扩展人员并具有联系方式。显示人员信息。但是联系信息不会显示。这是我的代码:

联系方式

package entities;

import interfaces.ContactDetailsInterface;
import java.io.Serializable;

public class Contact implements ContactDetailsInterface, Serializable {
private static final long serialVersionUID = 3458489886428771425L;
private String StreetLine;
private String City;
private String Parish;
private String Country;
private String Email;
private String Telephone;
private String TRN;


/**
 * @return the StreetLine
 */
@Override
public String getStreetLine() {
    return StreetLine;
}

/**
 * @param StreetLine the StreetLine to set
 */
@Override
public void setStreetLine(String StreetLine) {
    this.StreetLine = StreetLine;
}

/**
 * @return the City
 */
@Override
public String getCity() {
    return City;
}

/**
 * @param City the City to set
 */
@Override
public void setCity(String City) {
    this.City = City;
}

/**
 * @return the Parish
 */
@Override
public String getParish() {
    return Parish;
}

/**
 * @param Parish the Parish to set
 */
@Override
public void setParish(String Parish) {
    this.Parish = Parish;
}

/**
 * @return the Country
 */
@Override
public String getCountry() {
    return Country;
}

/**
 * @param Country the Country to set
 */
@Override
public void setCountry(String Country) {
    this.Country = Country;
}

/**
 * @return the Email
 */
@Override
public String getEmail() {
    return Email;
}

/**
 * @param Email the Email to set
 */
@Override
public void setEmail(String Email) {
    this.Email = Email;
}

/**
 * @return the Telephone
 */
@Override
public String getTelephone() {
    return Telephone;
}

/**
 * @param Telephone the Telephone to set
 */
@Override
public void setTelephone(String Telephone) {
    this.Telephone = Telephone;
}

@Override
public void setTRN(String trn) {
    this.TRN = trn;
}

@Override
public String getTRN() {
 return TRN; 
}
}

租户课程

package entities;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
import superclass.Person;

public class Tenant extends Person {

transient SimpleDateFormat dateFormat;
transient Calendar calendar;
transient Scanner in = new Scanner(System.in);

private String dateOfBirth;

//has a
private Contact contact;
private CalculatePayments payment;

private String registra;

public String getDateOfBirth() {
    return dateOfBirth;
}

/**
 * @param dateOfBirth the dateOfBirth to set
 */
public void setDateOfBirth(String dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

/**
 * @return the contact
 */
public Contact getContact() {
    return contact;
}

/**
 * @param contact the contact to set
 */
public void setContact(Contact contact) {
    this.contact = contact;
}

/**
 * @return the payment
 */
public CalculatePayments getPayment() {
    return payment;
}

/**
 * @param payment the payment to set
 */
public void setPayment(CalculatePayments payment) {
    this.payment = payment;
}

/**
 * @return the registrar
 */
public String getRegistra() {
    return registra;
}

/**
 * @param registra the registrar to set
 */
public void setRegistra(String registra) {
    this.registra = registra;
}

}

这是我要用来填充TableView的主要JavaFX控制器

MainPanelController

public class MainPanelSceneController implements Initializable {

@FXML
private TableView tenantTable;

/**
 * Initializes the controller class.
 *
 * @param url
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

    //firstName
    TableColumn<Tenant, String> fnColumn = new TableColumn<>("First Name");
    fnColumn.setCellValueFactory(new PropertyValueFactory<>("FirstName"));

    //lastName
    TableColumn<Tenant, String> lnColumn = new TableColumn<>("Last Name");
    lnColumn.setCellValueFactory(new PropertyValueFactory<>("LastName"));

    //gender
    TableColumn<Tenant, String> genderColumn = new TableColumn<>("Gender");
    genderColumn.setCellValueFactory(new PropertyValueFactory<>("Gender"));

    //dob
    TableColumn<Tenant, String> dobColumn = new TableColumn<>("D.O.B");
    dobColumn.setCellValueFactory(new PropertyValueFactory<>("dateOfBirth"));

    //Email
    TableColumn<Tenant, String> emailColumn = new TableColumn<>("Email");
    emailColumn.setCellValueFactory(new PropertyValueFactory<Tenant, String>("Email"));

    //Phone
    TableColumn<Tenant, String> phoneColumn = new TableColumn<>("Phone #");
    phoneColumn.setCellValueFactory(new PropertyValueFactory<>("Telephone"));

    //TRN
    TableColumn<Tenant, String> TRNColumn = new TableColumn<>("TRN");
    TRNColumn.setCellValueFactory(new PropertyValueFactory<>("TRN"));

    //Country
    TableColumn<Tenant, String> countryColumn = new TableColumn<>("Country");
    countryColumn.setCellValueFactory(new PropertyValueFactory<>("Country"));

    //Parish
    TableColumn<Tenant, String> parishColumn = new TableColumn<>("Parish");
    parishColumn.setCellValueFactory(new PropertyValueFactory<>("Parish"));

    //City
    TableColumn<Tenant, String> cityColumn = new TableColumn<>("City");
    cityColumn.setCellValueFactory(new PropertyValueFactory<>("City"));

    //StreetLine
    TableColumn<Tenant, String> streetColumn = new TableColumn<> 
 ("StreeLine");
    streetColumn.setCellValueFactory(new PropertyValueFactory<> 
   ("StreetLine"));

    tenantTable.setItems(getTenants());

    tenantTable.getColumns().addAll(fnColumn, lnColumn, genderColumn,
            dobColumn, emailColumn, TRNColumn,
            phoneColumn, countryColumn, parishColumn,
            cityColumn, streetColumn);
}

private ObservableList<Tenant> getTenants() {
    ObservableList<Tenant> observableList = 
FXCollections.observableArrayList();
    CrudOperations crud = new CrudOperations();
    ArrayList<Tenant> arr = (ArrayList<Tenant>) 
 crud.readObj(Files.TENANT_FILE);
    ListIterator itr = arr.listIterator();
    while (itr.hasNext()) {
        Tenant t = (Tenant) itr.next();
        observableList.add(t);
    }
    return observableList;
}

}

如前所述,observableList仅显示来自个人类的信息,而不会显示来自联系人类的信息。帮助!!!

1 个答案:

答案 0 :(得分:0)

JavaFX TableView PropertyValueFactory使用反射来找出要在表单元格中显示的值。

所以当你说:

cityColumn.setCellValueFactory(new PropertyValueFactory<>("City"));

这意味着cellValueFactory将尝试找到名为getCity()的方法来填充cityColumn中的单元格。

由于您将TableColumn定义为:

TableColumn<Tenant, String> cityColumn = new TableColumn<>("City");

工厂在“租户”类中寻找合适的吸气剂以填充单元格。您的问题是Tenant类需要具有getter,而Contact类却需要它们。我建议将联系吸气剂的包装材料添加到Tenant类中,即

public String getCity(){
  return contact.getCity();
}