我想用控制器中的数据填充TableView,但是我不知道为什么我的解决方案不起作用。
在fxml中,我要填充的元素看起来像这样:
<TableView fx:id="t" layoutX="0" layoutY="30" prefHeight="400" prefWidth="600">
<columns>
<TableColumn prefWidth="200" text="name" fx:id="name" />
<TableColumn prefWidth="200" text="adress" fx:id="adress" />
<TableColumn prefWidth="200" text="phone" fx:id="phone" />
</columns>
</TableView>
和我的控制者:
public class controler implements Initializable {
public Pane p;
public MenuBar menu;
@FXML public TableView<User> t;
@FXML public TableColumn<User, String> name;
@FXML public TableColumn<User, String> adress;
@FXML public TableColumn<User, String> phone;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
FXCollections.observableArrayList(new User("zaza","zaza","zaza"));
menu.prefWidthProperty().bind(p.widthProperty());
t.getItems().add(new User("John","NewYork","+16879060"));
t.setEditable(true);
t.setVisible(true);
}
}
我正在尝试向表中添加一些数据,但表中显示空列,我不知道哪里出错了
我认为这是完美的用户模型:
public class User {
private SimpleStringProperty name;
private SimpleStringProperty adress;
private SimpleStringProperty phone;
public String getName() {
return name.get();
}
public void setName(SimpleStringProperty name) {
this.name = name;
}
public String getAdress() {
return adress.get();
}
public void setAdress(SimpleStringProperty adress) {
this.adress = adress;
}
public String getPhone() {
return phone.get();
}
public void setPhone(SimpleStringProperty phone) {
this.phone = phone;
}
public User(String name, String adress, String phone) {
super();
this.name = new SimpleStringProperty(name);
this.adress = new SimpleStringProperty(adress);
this.phone = new SimpleStringProperty(phone);
}
}