【问题标题】:Java Windows Builder JList Load ConditionJava Windows Builder JList 加载条件
【发布时间】:2017-05-31 19:18:49
【问题描述】:

我正在创建一个医疗保健管理系统(患者、医生、专家...),我想通过显示专业的 ComboBox 的值过滤将出现在 JList 面板 (listEspecialistas) 中的专家。例如,Traumatology 是 ComboBox 的值,并且创伤专家将显示在列表面板中。这是我从 txt 文件(在代码中称为“especialistas”)加载专家的方式

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 在类中声明为上层:

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

这就是 txt 文件的结构方式:

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

例子:

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

还有几个其他专家的实例。

程序将文件加载到列表面板的方式完美运行(我知道效率不是很高)所以唯一的事情是添加条件,如果扫描的令牌之一等于 Traumatology,Cardiology(无论如果说)它只会拾取该行并将其添加到modelo。有什么建议么?谢谢。

【问题讨论】:

    标签: java windowsbuilder


    【解决方案1】:

    在添加到模型之前,您需要做的只是一个 if 检查以查看该行的实例化 Especialista 是否符合条件。

    我还修正了您可能犯的其他一些错误。

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

    【讨论】:

    • 完美运行@gabriel 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多