Java Windows 生成器 JList 加载条件
Java Windows Builder JList Load Condition
我正在创建一个医疗保健管理系统(患者、医生、专家...),我想通过显示专业的 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 在 class 中声明为上层:
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
还有几个其他专家的实例。
程序将文件加载到列表面板的方式完美无缺(我知道不是很有效)所以唯一的事情就是添加条件,如果扫描的令牌之一等于创伤学,心脏病学(无论if 说)它只会拾取该行并将其添加到 modelo
。有什么建议么?谢谢。
在添加到模型之前,您需要做的只是一个 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());
}
}
}
我正在创建一个医疗保健管理系统(患者、医生、专家...),我想通过显示专业的 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 在 class 中声明为上层:
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
还有几个其他专家的实例。
程序将文件加载到列表面板的方式完美无缺(我知道不是很有效)所以唯一的事情就是添加条件,如果扫描的令牌之一等于创伤学,心脏病学(无论if 说)它只会拾取该行并将其添加到 modelo
。有什么建议么?谢谢。
在添加到模型之前,您需要做的只是一个 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());
}
}
}