Search code examples
javauitableviewjavafxempty-list

Tableview doesn't populate JavaFX


so i have seen all the already asked questions, and tried them, but it doesn't work.

Here is my code:

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


Solution

  • You're using the wrong type parameters for TableColumn and TableView. Since the items of the TableView are of type Client (at least that seems to be the intention), you should use TableView<Client> and TableColumn<Client, String>.

    Furthermore you're not setting the TableView items.

    Also you're using TableColumn.cellValueFactory wrong. The cellValueFactory is used for every row that TableView displays on screen. Using values of the last Client results in the values from the last item being used for every non-empty row. Instead you should use the values provided by the TableColumn.CellDataFeatures passed to the Callback.

    @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);
    }