Is there a way to get all elements in a JList?

Matthew Pigram picture Matthew Pigram · May 28, 2012 · Viewed 32k times · Source

I was wondering if there is a way to retrieve a list of all the elements that have been added to a JList. For example I would like JList to return an array or list of Strings or JLabels from a custom cell renderer.

Answer

Mikita Belahlazau picture Mikita Belahlazau · May 28, 2012

You can use getModel method to get ListModel. And then use getElementAt and getSize method to build array or list or whatever you want.