Tableview 不填充 JavaFX
Tableview doesn't populate JavaFX
所以我已经看到了所有已经提出的问题,并尝试了它们,但是它不起作用。
这是我的代码:
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());
}
}
}
enter image description here
您为 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);
}
所以我已经看到了所有已经提出的问题,并尝试了它们,但是它不起作用。
这是我的代码:
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());
}
}
}
enter image description here
您为 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);
}