如何从jlist中检索对象列表

时间:2016-06-28 06:28:03

标签: object jlist multi-select

我有一个Jlist填充了-Integer的哈希图,“Obra” - 对象,我有一个接收obras列表的方法,我试图获取该Jlist中的所有选择的obras并​​尝试通过作为我的方法的参数,但我不断得到类型不匹配的错误

这是我的代码

public class Emprestimo {
private Usuario u;
private Calendar dataRealizacao;
private double total = 0;

public Emprestimo(Usuario u){
    this.u = u;
    dataRealizacao = Calendar.getInstance();
}

public void realizaEmprestimo(Obra... o){
    try{...do stuff


anotherclass
emp = new Emprestimo(user);
emp.realizaEmprestimo(JlistObra.getSelectedValuesList()); // here's where i get my error

我试图像这样做演员

(emp.realizaEmprestimo((Obra)JlistObra.getSelectedValuesList());

和这个

(emp.realizaEmprestimo((Obra[])JlistObra.getSelectedValuesList());

但它不起作用

我完成了:

emp.realizaEmprestimo((Obra) JlistObra.getSelectedValue());

可行,但我的Jlist中只有一个select元素

1 个答案:

答案 0 :(得分:0)

我使它工作,而不是最好的方式,但解决方案是一个解决方案:

int[] indices = JlistObra.getSelectedIndices();

        for (int i : indices){
            emp.realizaEmprestimo((Obra)JlistObra.getModel().getElementAt(i));
        }