0

我想选择 JList 中特定于患者已经单击的项目。如果我点击第三位患者,JList 将选择患者选择的项目。

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;
    }   
}
4

2 回答 2

2

我怀疑您通过的列表是字符串列表,而不是您在此处假设的条件列表:

for(Conditions p: PassedIn)

然而,你给我们的东西很少。完整的错误信息是什么?

另外,尝试将 的类型更改MakePatientJListSelectedArrayList<Condition>。我想你会发现编译器会告诉你它需要一个ArrayList<String>

于 2013-03-02T21:27:32.593 回答
2

您的方法采用输入 ArrayList 您必须向下转换它

ArrayList<Conditions> PassedIn = (ArrayList<Conditions>)PatientConList;
于 2013-03-02T21:29:10.423 回答