samedi 27 juin 2015

Populate a JCombo Box using Array from Another Class in Java

First of all this is the code that I currently have:

 //adds each organism to the JComboBox with their corresponding organism id for later use 
    //Currently if user doesn't click on 1st option an error occurs.. change this error - perhaps display error message
    Organism organisms[] = new Organism[224];
    organisms[0] = new Organism("Please Select an Organism...", "");
    organisms[1] = new Organism("Acacia auriculiformis", ">aau");
    organisms[2] = new Organism("Acacia mangium", ">amg");
    organisms[3] = new Organism("Acyrthosiphon pisum", ">api");
    organisms[4] = new Organism("Aedes aegypti", ">aae");
    organisms[5] = new Organism("Aegilops tauschii", ">ata");
    organisms[6] = new Organism("Amborella trichopoda", ">atr");
    organisms[7] = new Organism("Amphimedon queenslandica", ">aqu");
    organisms[8] = new Organism("Anolis carolinensis", ">aca");
    organisms[9] = new Organism("Anopheles gambiae", ">aga");
    organisms[10] = new Organism("Apis mellifera", ">ame");

....... etc etc

    selectOrganism = new JComboBox(organisms);
    selectOrganism.setBounds(10, 10, 330, 25);
    add(selectOrganism);
    selectOrganism.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            Organism o = (Organism) e.getItem();
            organismId = o.getId();
            //System.out.println(organismId);
            //label.setText("You selected customer id: " + o.getId());

        }

    });

Then in another class I have this:

public class Organism{
    private String name;
    public String id;

    public Organism(String name, String id){
        this.name = name;
        this.id = id;
    }

    public String toString(){
        return getName();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

This code displays the organisms (i.e. Arabidopsis thaliana) in the JCombo box and when the organism is selected from the JCombo box the id (i.e. >ath) is selected for use elsewhere. However as you can probably see this code is extremely messy as most of it is hard coded.

Therefore in a new class I have manipulated a text file in order to retrieve the required information into an array:

public static void main(String[] args) throws IOException{
    BufferedReader bReader = new BufferedReader(new FileReader("organisms.txt"));

    bReader.readLine(); // this will read the first line


    String line = null;
    while ((line = bReader.readLine()) != null){

        //removes the first space 
        String test = line.replaceFirst("\\s+", "");


        //used regex lookaround to split wherever there are 3 capital letters present, or where any of the tree are present

        String[] peopleInfo = test.split("(\\p{Upper}(?=\\p{Upper})\\p{Upper}\\p{Upper})|(?=Chromalveolata)|(?=Metazoa)|(?=Mycetozoa)|(?=Viridiplantae)|(?=Viruses)|\\;");

        //creates arrays for id, organism and tree
        String id = ">" + peopleInfo[0];
        String organism = peopleInfo[1];
        String tree = peopleInfo[2].replaceAll(";", "").replaceAll("\\d+.*", "");
       System.out.println(tree);
}
    }

Therefore my question is how do I go about populating my JCombo box with organism array and then associating each of the ids from the id array with the appropriate organism from my organism array.

I think there's probably a relatively simple solution however I'm new to Java so I'm having a little trouble.

Sorry for the long post but thought that if you could see the code it may make it easier for you to help.

Any help would be greatly appreciated.

Thanks :)

Aucun commentaire:

Enregistrer un commentaire