Home > Software engineering >  populate combobox with part of values from json
populate combobox with part of values from json

Time:01-07

I'm trying to fill a combobox with a specific field (name) from a json file.

parser file to extract information from json:

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

public class ReferencesParser {

    private final File jsonFile;

    public ReferencesParser(File jsonFile) {
        this.jsonFile = jsonFile;
    }

    public Map<ReferencesEnum, Reference> parseReferenceFile() {

        Map<ReferencesEnum, Reference> referencesMap = new HashMap<>();

        try {
            String content = new String(Files.readAllBytes(this.jsonFile.toPath()));
            JSONObject json = new JSONObject(content);
            for (Object object : (JSONArray) json.get("genes")) {
                JSONObject geneObject = (JSONObject) object;

                Long id = geneObject.getLong("id");
                String name = geneObject.getString("name");
                String sequence = geneObject.getString("sequence");

                Reference reference = new Reference(id, name, sequence);
                referencesMap.put(ReferencesEnum.valueOf(name), reference);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return referencesMap;
    }

}

genes.json file containing data to implement:

{
  "genes": [
    { "id":"1","name":"gene1", "sequence": "gcattgtgggcgctatattgt" },
    { "id":"2","name":"gene2", "sequence": "gcattgtgggcgctatattcc" },
    { "id":"3","name":"gene3", "sequence": "gcatcgtgggcgctatatcat" }
  ]
}

and I'm trying to populate a combobox with 'name' value from json using a controller file:

...
    @FXML
    private ComboBox<String> choosegene;

    @FXML
    public void initialize(){

        try {
            populateGene_reference();
//          System.out.println(geneFile);
            System.out.println(referencesMap);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CompoundNotFoundException e) {
            e.printStackTrace();
        }
    };

    private void populateGene_reference() throws IOException, CompoundNotFoundException {
        URL url = getClass().getResource("/genes.json");
            if (url != null) {
                File geneFile = new File(url.getPath());
//              String _Path = url.getPath();
//              System.out.println("URL = "   url);
                ReferencesParser parser = new ReferencesParser(geneFile);
//              System.out.println("genefile = "   geneFile);
                Map<ReferencesEnum, Reference> referencesMap = parser.parseReferenceFile();
//              Map<ReferencesEnum, Reference> test parser.parseReferenceFile();
                System.out.println("refmap = "   referencesMap);
                choosegene.getItems().add(String.valueOf(referencesMap));

I have tried different ways to get my gene names but 'system.out.println' give me this:

refmap = {gene2=gene2 gcatcgtgggcgctatatcat}

refmap2 = {}

What did I miss?

Thank you for your help

CodePudding user response:

ok referencesMap was ok but not choosegene, this works for me:

private void populateGene_reference() throws IOException, CompoundNotFoundException {
    URL url = getClass().getResource("/main/genes.json");
if (url != null) {
    File geneFile = new File(url.getPath());
    ReferencesParser parser = new ReferencesParser(geneFile);
    Map<ReferencesEnum, Reference> referencesMap = parser.parseReferenceFile();

        for (ReferencesEnum key : referencesMap.keySet()) {
            choosegene.getItems().add(referencesMap.get(key).getName());
        }

Hope it will help those having the same issue!

  • Related