2017-05-31 91 views
0

即時創建一個衛生保健管理系統(患者,醫師,專家...),我想篩選專家出現在一個JList面板(listEspecialistas)的值組合框將展示特色。例如,創傷學是ComboBox的價值,創傷專家將顯示在列表面板中。這是我加載從一個txt文件,專家的方式(稱爲代碼「especialistas」)Java的Windows生成器JList的負載條件

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

    } 

}

莫德洛聲明上的類:

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

這是txt文件的結構方式:

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

實施例:

Francisco;Lopez Navarro;10:00/14:00;[email protected];956325485;Traumatología;/presentacion/Imagenes/Especialistas/paco-126.png 

還有其他幾個專家的實例。

程序加載文件到列表面板的方式是完美的(不是很有效,我知道),所以唯一的辦法是添加條件,如果其中一個令牌被掃描等於創傷學,心臟病學(不管如果說)它只會選擇該行,並將其添加到modelo。有什麼建議麼?謝謝。

回答

0

所有你需要做的就是檢查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()); 
     } 

    } 
} 
+0

完美的作品@gabriel非常感謝你! – Supersoaker