Getting back data from JList

时光怂恿深爱的人放手 提交于 2019-12-05 01:20:48
Varun

You have to use the getModel() method to get the model data and then use the methods inside ListModel in order to get all the data elements.

ListModel model = list.getModel();

for(int i=0; i < model.getSize(); i++){
     Object o =  model.getElementAt(i);  
}

http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html#getModel()

http://docs.oracle.com/javase/6/docs/api/javax/swing/ListModel.html

To get the selections, you will need to use a combination of getModel and getSelectedIndices

ListModel model = jListInstance.getModel();

for(int index : jListInstance.getSelectedIndices()) {
    System.out.println(model.getElementAt(index));
}

Use the getModel() method to retrieve the data model that is contained within the JList. The List model can be traversed in the following manner:

ListModel list = jListObj.getModel();
for(int i = 0; i < list.getSize(); i++){
     Object obj = list.getElemenetAt(i);
}

http://docs.oracle.com/javase/6/docs/api/javax/swing/ListModel.html http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html#getModel%28%29

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!