简体   繁体   中英

Java Windows Builder JList Load Condition

im creating a Health Care manage system (Patients, Medics, Specialists...) and I want to filter the specialists that will appear in a JList Pannel (listEspecialistas) by the value of a ComboBox that will show the specialities. For example, Traumatology is the value of the ComboBox and the trauma specialists will be shown in the listpanel. This is the way i load the specialists from a txt file (called "especialistas" in the code)

private void filtrarPor(String especialidad){//filterBy
    //If cbEspecialidades-getSelectedItem()/ComboBoxValue==Traumatology
    if(cbEspecialidades.getSelectedItem().equals("Traumatología")){
        Scanner sc;
        Especialista aux;
        StringTokenizer st;

        try {
            sc = new Scanner (especialistas);
            sc.nextLine();
            while (sc.hasNextLine()) {
                st = new StringTokenizer(sc.nextLine(), ";"); 
                while (st.hasMoreTokens() && st.equals("Traumatología")) {

                    aux = new Especialista (st.nextToken(), st.nextToken(), st.nextToken(),
                        st.nextToken(), st.nextToken(), st.nextToken(),new ImageIcon(Pacientes.class.getResource(st.nextToken())));

                    modelo.addElement(aux);

                }

                listEspecialistas.setModel(modelo);
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }   

}

Modelo is declared upper in the class:

DefaultListModel <Especialista> modelo = new DefaultListModel <Especialista>();

And this is the way the txt file is structured:

Name;Surname;Schedule;email;phoneNumber;Speciality;profile picture route

Example:

Francisco;Lopez Navarro;10:00/14:00;franciscolopez@gmail.com;956325485;Traumatología;/presentacion/Imagenes/Especialistas/paco-126.png

And several more instances of other specialists.

The way the progam loads the file into the list panel works perfectly(not very efficient i know) so the only thing would be to add the condition that if one of the Tokens scanned is equals to Traumatology,Cardiology (Whatever the if says) it will only pick up that line and will add it to modelo . Any suggestions? Thank you.

All you need to do is an if check to see if the instantiated Especialista for the line matches the criteria before to add to the model.

I fixed some other mistakes that you might have done also.

private void filtrarPor(String especialidad){
        Scanner sc;
        Especialista aux;
        StringTokenizer st;

        try {
            sc = new Scanner (especialistas);
            sc.nextLine();
            while (sc.hasNextLine()) {
                st = new StringTokenizer(sc.nextLine(), ";"); 
                if(st.countTokens() >= 7) { //skip not valid Especialista

                    aux = new Especialista (st.nextToken(), st.nextToken(), st.nextToken(),
                        st.nextToken(), st.nextToken(), st.nextToken(),new ImageIcon(Pacientes.class.getResource(st.nextToken())));

                    if(aux.getEspecialidade().equals(especialidad))
                        modelo.addElement(aux);

                }
            }
            listEspecialistas.setModel(modelo);//you can set the model with your list after everything is loaded
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM