Max Kühn
Max Kühn

Reputation: 1

Possible to get all Items from JList?

I know it is possible to get all selected Items from a JList with:

leftlist.getSelectedValues();

But is it possible to get all Items and not only the selected ones?

Upvotes: 0

Views: 1843

Answers (1)

Blasanka
Blasanka

Reputation: 22437

You need to use getModel() and getSize() to catch the length of the JList. Also you need a loop to iterate through the indexes to get elements(getElementAt()).

Example:

for(int i = 0; i< jList.getModel().getSize();i++){
    System.out.println(jList.getModel().getElementAt(i));
}

You are right, you can get selected values from jList.getSelectedValues(). Also it is possible to get multiple selected elements in JList by jList.getSelectedValuesList() or listen for events on the list's ListSelectionModel. Read the documentation for How to Use Lists.

Multiple selection demo from oracle.

Upvotes: 1

Related Questions