我想选择特定于患者已经点击的JList中的项目。如果我点击第三位患者,JList将选择患者选择的项目。Java Casting Object to Type?
在PatientConList中是该患者的条件对象。
当你添加一个病人你这样做:
List PatientConditions1 = new ArrayList();
PatientConditions1 = ConditionsJList.getSelectedValuesList();
然后当你点击一个病人它这样做
public void MakePatientJListSelected(ArrayList PatientConList) {
//this makes selected, so get condition array off patient.
//so call this from patient jlist listener
ConditionsJList.clearSelection();
//get number - 1, so then 0 = first
ArrayList<Conditions> PassedIn = PatientConList;
ArrayList SelectList = new ArrayList();
for(Conditions p: PassedIn)
{
int Num = p.getNumber();
Num = Num - 1;
SelectList.add(Num);
}
//int startSel = 0;
//int endSel = 0;
//ConditionsJList.setSelectionInterval(startSel, endSel);
for(int k=0;k<PassedIn.size();k++)
{
int startSel2 = k;
int endSel2 = k;
ConditionsJList.addSelectionInterval(startSel2, endSel2);
}
}
对不起,基本上我有一个JList和你可以选择你想要的条件并点击添加。这为患者增加了条件列表。
基本上,当您选择一个患者进行查看时,我需要它,以便在jlist上选择患者的jlist条件。
即时得到这个错误的异常在线程 “AWT-EventQueue的-0” java.lang.ClassCastException:java.lang.String中不能转换到DentistAppNew.Conditions
我的病人类:
import java.util.*;
public class Patient {
private int AccountNumber;
private String Name;
private int Age;
private String Address;
private String Sex;
private String Phone;
private List<Conditions> ConditionNames;
public Patient(int AccountNumber1,
String Name1,
int Age1,
String Address1,
String Sex1,
String Phone1,
List<Conditions> ConditionNames1)
{
this.AccountNumber = AccountNumber1;
this.Name = Name1;
this.Age = Age1;
this.Address = Address1;
this.Sex = Sex1;
this.Phone = Phone1;
this.ConditionNames = ConditionNames1
}
public int getAccountNumber() { return AccountNumber; }
public String getName() { return Name; }
public int getAge() { return Age; }
public String getAddress() { return Address; }
public String getSex() { return Sex; }
public String getPhone() { return Phone; }
public List getConditionsList() { return ConditionNames; }
public void setName(String Name1) { this.Name = Name1; }
public void setAge(int Age1) { this.Age = Age1; }
public void setAddress(String Address1) { this.Address = Address1; }
public void setSex(String Sex1) { this.Sex = Sex1; }
public void setPhone(String Phone1) { this.Phone = Phone1; }
}
我的条件类:
public class Conditions {
private int Number;
private String Name;
public Conditions(int Number1,String Name1) {
this.Number = Number1;
this.Name = Name1;
}
public int getNumber() {
return Number;
}
public String getName() {
return Name;
}
}
哪一行,你得到的是例外? – Jeffrey 2013-03-02 21:23:38
这就是为什么你应该使用泛型......如果你不使用原始类型,你会在* compile *时间出现这个错误。 – 2013-03-02 21:28:03
它在第一个循环! – 2013-03-02 22:42:57