table view does not display datas javafx

i’m working on a project application that should display X509certificates details in a TableView. The certificates files are DER encoded and are loaded from a specific folder of the client computer. When he clicks on the HDD icon in the app window, i call the handleHDD() function of the controller to load certificates and display their details on the TabeView.

Here is the code of the function handleHDD() from my controller:

@FXML
private void handleHDD() throws CertificateException, IOException, NoSuchProviderException{
    String userDir = System.getProperty("user.home");
    File folder = new File(userDir +"//Desktop//Certificate_Folder");
    FilenameFilter filter = new MyFileFilter();

    File[] certificates = folder.listFiles(filter);

    ObservableList<CertificateModel> data = FXCollections.observableArrayList();
    String columnHeader[] = {"Nom","Version","N°série","Algorithme de signature","Emetteur","Valide à partir de","Valide jusqu'au","Objet","Clé publique"};
    if (certificates!=null){
        int sizeColumns = 9;

        for (File file : certificates){
            if(file.isFile()){
            try{

            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            FileInputStream input = new FileInputStream(file);

            X509Certificate cert = (X509Certificate)cf.generateCertificate(input);

            for (int j = 0; j < sizeColumns; j++) {
                TableColumn<CertificateModel, String> col = new TableColumn();
                col.setText(columnHeader[j]);
                col.setMinWidth(200);

                col.setCellValueFactory(new PropertyValueFactory<CertificateModel, String>(columnHeader[j]));
                table.getColumns().addAll(col);

            }

            data.add(new CertificateModel(file.getName().replace(".der", ""), cert.getVersion(),cert.getSerialNumber().toString(16), cert.getSigAlgName(), cert.getIssuerDN().toString(), cert.getNotBefore(), cert.getNotAfter(), cert.getSubjectDN().toString(), cert.getPublicKey().toString()));
            table.setItems(data);
            }catch (IOException e){
                e.printStackTrace();
            }
            }

            }

        }
    }

And there is my CertificateModel.java class:

public class CertificateModel {

private String nom;
private int version;
private String numserie;
private String algosign;
private String emetteur;
private Date validfrom;
private Date validto;
private String objet;
private String clepub;

public CertificateModel(String nom, int type, String numserie, String algosign, String emetteur, Date validfrom, Date validto, String objet, String clepub) {
    this.nom = nom;
    this.version = type;
    this.numserie = numserie;
    this.algosign = algosign;
    this.emetteur = emetteur;
    this.validfrom = validfrom;
    this.validto = validto;
    this.objet = objet;
    this.clepub = clepub;
}

public void setNom(String nom) {
    this.nom = nom;
}

public void setType(int type) {
    this.version = type;
}

public void setNumserie(String numserie) {
    this.numserie = numserie;
}

public void setAlgosign(String algosign) {
    this.algosign = algosign;
}

public void setEmetteur(String emetteur) {
    this.emetteur = emetteur;
}

public void setValidfrom(Date validfrom) {
    this.validfrom = validfrom;
}

public void setValidto(Date validto) {
    this.validto = validto;
}

public void setObjet(String objet) {
    this.objet = objet;
}

public void setClepub(String clepub) {
    this.clepub = clepub;
}

private static final Logger LOG = Logger.getLogger(CertificateModel.class.getName());

public String getNom() {
    return nom;
}

public int getType() {
    return version;
}

public String getNumserie() {
    return numserie;
}

public String getAlgosign() {
    return algosign;
}

public String getEmetteur() {
    return emetteur;
}

public Date getValidfrom() {
    return validfrom;
}

public Date getValidto() {
    return validto;
}

public String getObjet() {
    return objet;
}

public String getClepub() {
    return clepub;
}

public static Logger getLOG() {
    return LOG;
}

}

When i inspect my code in debug mode, i can see all my datas fine inside the variables but the tableview is still empty.

Help please!

if you know exactly there are 9 columns better to create a separate column for each of them instead of creating them in a loop. So you can ad them in an .fxml file. Then you have to do a few things:

  • refactor your model class, and add there Property-s for those values you want to display in the table instead of simple Objects, like:

    private String nom; ->private StringProperty nom;
    private int version; -> private IntegerProperty version
    . . .
    private Date validfrom; -> private ObjectProperty<Date> validfrom;
    . . .
    

    then getters for them.

  • set the cellValueFactory for each column this way:

    nomColumn.setCellValueFactory(data -> data.getValue().nomProperty());
    

    Then it should work .

If you are using this way your code will be more understandeable, and clear, and you can see where what is happening.