我有5个列和3行的tableview。现在我想填写第1行和第2行的数据。请给我看一些示例代码。
这是我的代码 person.java
public class Person {
static int x = 1;
private final SimpleStringProperty firstName = new SimpleStringProperty("");
private final SimpleStringProperty lastName = new SimpleStringProperty("");
private final SimpleStringProperty emailid = new SimpleStringProperty("");
private final Integer iNumber = new Integer(x);
public Person(){
this("","","");
}
public Person(String fn,String ln,String eid){
x++;
setFirstName(fn);
setLastName(ln);
setEmailId(eid);
}
public void setFirstName(String fn){
firstName.set(fn);
}
public void setLastName(String ln){
lastName.set(ln);
}
public void setEmailId(String id){
emailid.set(id);
}
public String getFirstName(){
return firstName.get();
}
public String getLastName(){
return lastName.get();
}
public String getEmailId(){
return emailid.get();
}
}
AddressBookDocumentController.java代码
public class AddressBookDocumentController implements Initializable {
public AddressBookDocumentController(){
this.personData = FXCollections.observableArrayList(
new Person("X","Y","Q"),
new Person("A","B","W"),
new Person("HAi","Bye","E")
);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
firstName.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
lastName.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));
emailId.setCellValueFactory(new PropertyValueFactory<Person,String>("emailId"));
tableId.setItems(personData);
}
final ObservableList<Person> personData ;
@FXML TableView<Person> tableId;
@FXML TableColumn<Person, String> firstName;
@FXML TableColumn<Person,String> lastName;
@FXML TableColumn<Person,String>emailId;
@FXML TextField tfName;
@FXML TextField tlName;
@FXML TextField tEmail;
@FXML public void handleButtonSave(){
tableId.getItems().get(0).setFirstName("JOHN"); //HERE CHANGES SHOULD REFLECT BUT IT's NOT HAPPENING
}
}
谢谢, 帕。
答案 0 :(得分:1)
将以下JavaFX特定的getter添加到Person类,然后重试:
public SimpleStringProperty firstNameProperty(){
return firstName;
}
public SimpleStringProperty lastNameProperty(){
return lastName;
}
public SimpleStringProperty emailidProperty(){
return emailid;
}
有关详细信息,请参阅
Binding a Label's text property (in an FXML file) to an IntegerProperty (in a controller)和
Autoupdating rows in TableView from model
答案 1 :(得分:0)
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("", "Smith", ""),
);
TableColumn col1= new TableColumn("Column1");
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("Column1"));
TableColumn col2= new TableColumn("Column2");
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("Column2"));
TableColumn col3= new TableColumn("Column3");
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("Column3"));
table.setItems(data);
table.getColumns().addAll(col1, col2, col3);