我在ContactList上做类似的程序,但是当我尝试添加信息时,我无法看到我的信息。但如果我点击9次,我就可以看到滚动。 我有我的工作文件,在下面结束prtsc fxml文件。
@FXML
private void initialize() throws IOException {
iniData();
id.setCellValueFactory(new PropertyValueFactory<Work, Integer>("id"));
Fname.setCellValueFactory(new PropertyValueFactory<Work, String>("fname"));
Lname.setCellValueFactory(new PropertyValueFactory<Work, String>("lname"));
Phone.setCellValueFactory(new PropertyValueFactory<Work, String>("phone"));
Email.setCellValueFactory(new PropertyValueFactory<Work, String>("email"));
table.setItems(arr);
addName.setPromptText("fname");
addName.setMaxWidth(Fname.getPrefWidth());
addLName.setPromptText("lname");
addLName.setMaxWidth(Lname.getPrefWidth());
addPhone.setPromptText("phone");
addPhone.setMaxWidth(Phone.getPrefWidth());
addEmail.setPromptText("email");
addEmail.setMaxWidth(Email.getPrefWidth());
btn01.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
arr.add(new Work(addName.getText(),
addLName.getText(), addPhone.getText(), addEmail.getText()));
} } ); }
public void iniData() throws IOException {
arr.add(new Work(1, "Alex", "qwerty", "33333","alex@mail.com"));
arr.add(new Work(2, "Bob", "dsfsdfw","0987432", "bob@mail.com"));
arr.add(new Work(3, "Jeck", "dsfdsfwe","345743", "Jeck@mail.com"));
arr.add(new Work(4, "Mike", "iueern","32456", "mike@mail.com"));
arr.add(new Work(5, "colin", "woeirn","12233455", "colin@mail.com"));
}}
我的工作java文件。
public class Work implements Serializable{
private Integer id;
private String Fname;
private String Lname;
private String Phone;
private String Email;
private String addFname;
private String addLname;
private String addPhone;
private String addEmail;
public Work(Integer id, String fname, String lname, String phone, String email) {
this.id = id;
Fname = fname;
Lname = lname;
Phone = phone;
Email = email;
}
public Work(String addFname, String addLname, String addPhone, String addEmail) {
this.addFname = addFname;
this.addLname = addLname;
this.addPhone = addPhone;
this.addEmail = addEmail;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFname() {
return Fname;
}
public void setFname(String fname) {
Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
Lname = lname;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getAddFname() {
return addFname;
}
public void setAddFname(String addFname) {
this.addFname = addFname;
}
public String getAddLname() {
return addLname;
}
public void setAddLname(String addLname) {
this.addLname = addLname;
}
public String getAddPhone() {
return addPhone;
}
public void setAddPhone(String addPhone) {
this.addPhone = addPhone;
}
public String getAddEmail() {
return addEmail;
}
public void setAddEmail(String addEmail) {
this.addEmail = addEmail;
}
}
答案 0 :(得分:0)
由于您未在Work类中使用属性,因此必须将单元格设置为
FName.setCellValueFactory(new Callback<CellDataFeatures<Work, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Work, String> p) {
return new ReadOnlyObjectWrapper(p.getValue().getFname());
}
});
作为替代,你可以使用属性,但是它们不是可序列化的,你必须覆盖读取和写入对象的函数,如here