所以我看到了所有已经问过的问题,并尝试了它们,但它不起作用。
这是我的代码:
public class SelectClientInterface implements Initializable {
//Views
@FXML
TableView<String> selectClientTable;
@FXML
TableColumn<String, String> CodeC, NomC, RegCommerceC, IdFiscalC, NArtFisC;
@FXML
Button selectClientButton, returnSelectClientButton;
ObservableList<Clients> mylist = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
populateTable();
}
void populateTable() {
mylist.addAll(clientsArray);
for (Clients client : clientsArray) {
CodeC.setCellValueFactory(c -> new SimpleStringProperty(String.valueOf(client.getCodeC())));
NomC.setCellValueFactory(c -> new SimpleStringProperty(client.getNomC()));
RegCommerceC.setCellValueFactory(c -> new SimpleStringProperty(client.getRegCom()));
IdFiscalC.setCellValueFactory(c -> new SimpleStringProperty(client.getIdFiscal()));
NArtFisC.setCellValueFactory(c -> new SimpleStringProperty(client.getnArticleFiscal()));
System.out.println(String.valueOf(client.getCodeC()) + client.getNomC() + client.getRegCom() + client.getIdFiscal() + client.getnArticleFiscal());
}
}
}
在此处输入图像描述
您使用了错误的类型参数来TableColumn
和TableView
。由于TableView
的项目属于Client
类型(至少这似乎是意图(,因此您应该使用TableView<Client>
和TableColumn<Client, String>
.
此外,您没有设置TableView
项。
你也用错TableColumn.cellValueFactory
。cellValueFactory
用于屏幕上显示TableView
的每一行。使用最后一个Client
的值会导致最后一个项的值用于每个非空行。相反,您应该使用传递给Callback
的TableColumn.CellDataFeatures
提供的值。
@FXML
TableView<Client> selectClientTable;
@FXML
TableColumn<Client, String> CodeC, NomC, RegCommerceC, IdFiscalC, NArtFisC;
@FXML
Button selectClientButton, returnSelectClientButton;
ObservableList<Clients> mylist = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
populateTable();
}
void populateTable() {
mylist.addAll(clientsArray);
CodeC.setCellValueFactory(c -> new SimpleStringProperty(String.valueOf(c.getValue().getCodeC())));
NomC.setCellValueFactory(c -> new SimpleStringProperty(c.getValue().getNomC()));
RegCommerceC.setCellValueFactory(c -> new SimpleStringProperty(c.getValue().getRegCom()));
IdFiscalC.setCellValueFactory(c -> new SimpleStringProperty(c.getValue().getIdFiscal()));
NArtFisC.setCellValueFactory(c -> new SimpleStringProperty(c.getValue().getnArticleFiscal()));
for (Clients client : clientsArray) {
System.out.println(String.valueOf(client.getCodeC()) + client.getNomC() + client.getRegCom() + client.getIdFiscal() + client.getnArticleFiscal());
}
selectClientTable.setItems(mylist);
}