How can I get text from a component in a JList?

I have JList, and I want to get the text of the record of this list at a specific index. Can someone tell me how to do this, or do I need to change the structure of the code to getValuesinstead getIndices?

+3
source share
4 answers
JList dataList=(...)

 for(int i = 0; i < dataList.getModel().getSize(); i++) {
     System.out.println(dataList.getModel().getElementAt(i));
 }
+8
source
Object[] temp = jList1.getSelectedValues();
temp[i] = the object you want.
+3
source
DefaultListModel list = new DefaultListModel();
JList jl = new JList(list);

int i = 21;
Object = element;
String = yourElement;

element = jl.getModel().getElementAt(i);
yourElement = element.toString;
+2
source
String nick = jListNicknames.getModel().getElementAt(index).toString();
System.out.println(nick);
0
source

Source: https://habr.com/ru/post/1710018/


All Articles