我想選擇特定於患者已經點擊的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