0

假设我有一个包含十个项目的列表框,然后我为每个页面设置五个项目,所以现在列表框有两个页面

问题是每次我想获取列表框中的值时,它只会得到五个项目而不是十个项目。

我在我的列表框上使用 ListItemRenderer

下面是我的代码:

AnnotationConfiguration config = new AnnotationConfiguration();
SessionFactory sessionFactory = config.configure().buildSessionFactory();

Session session = sessionFactory.openSession();
String str = "from Users";
Query q = session.createQuery(str);

List<Users> user = q.list();
session.close();
sessionFactory.close();

list.setModel(new ListModelList<Users>(user));
list.setItemRenderer(new ListitemRenderer() {

    @Override
    public void render(Listitem item, Object data, int index)
            throws Exception {
        Users user = (Users) data;
        item.setValue(user);

        new Listcell(user.getUsername()).setParent(item);
        new Listcell(user.getUserrole()).setParent(item);
    }
});

}

在我的代码下方获取值:

String name ="";
String role ="";

Users user = new Users();
Listbox box = (Listbox) list.getFellow("win").getFellow("list");
List<Listitem> lists = box.getItems();

//membaca setiap row yang ada pada list
for(Listitem currentList : lists){

    List<Component> com = currentList.getChildren();

    //membaca setiap column pada row
    for (Component currentComp : com){
        Listcell lc = (Listcell) currentComp;
        if(lc.getColumnIndex() == 0){
            name = lc.getLabel();
            System.out.println("name = " + name);
        }

        if(lc.getColumnIndex()==1){
            role = lc.getLabel();
            System.out.println("role = " + role);
        }
    }

}
4

1 回答 1

1

这是正确的行为。正如您所描述的,列表是View并且View
永远不会超过 5 个Items

你想得到Model所以得到Model

ListModelList lists = (ListModelList)box.getModel();
于 2013-03-26T03:54:47.633 回答