Sorting a JList

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Daniel Pitts

Dirk said:
Any neat way to do this?
I can decant the (String) contents into an array, do Array.sort() and
reload the JList, but that seems a big ugly
Actually, that seems to be a fairly good way to approach it.

Alternatively, you can create your own "SortableListModel", and add a
"sort()" method to it. You might even come up with an elegant sort
algorithm that fire insert/remove only for the smallest number of
changes necessary to achieve the sort. However, I don't think it would
help you out that much.

Another approach would be to create your own "SortedListmModel" which
keeps everything in an ArrayList, and uses Collections.binarySearch to
maintain sorted order within the list. That way, your list is always
sorted.
 
D

Dirk Bruere at NeoPax

Daniel said:
Actually, that seems to be a fairly good way to approach it.

Alternatively, you can create your own "SortableListModel", and add a
"sort()" method to it. You might even come up with an elegant sort
algorithm that fire insert/remove only for the smallest number of
changes necessary to achieve the sort. However, I don't think it would
help you out that much.

Another approach would be to create your own "SortedListmModel" which
keeps everything in an ArrayList, and uses Collections.binarySearch to
maintain sorted order within the list. That way, your list is always
sorted.

I've already done it as I outlined above.
However, I'm slightly puzzled as to why there is no sort method in JList.

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
E

Eric Sosman

Dirk said:
[...]
However, I'm slightly puzzled as to why there is no sort method in JList.

Because a JList is a view of data that lives in a ListModel.
If the ListModel wants to sort its data, or shuffle its data, or
filter, curdle, or diddle its data, that's the business of the
ListModel and not of the JList. JList is just an onlooker.

You really need to spend some more time getting a grip on
this model-versus-view notion. Right now, your grip is, er, an
exemplar of excessive relaxation.
 
R

Roedy Green

I've already done it as I outlined above.
However, I'm slightly puzzled as to why there is no sort method in JList.

It is a division of labour. JList just displays data. The ListModel
is in charge of the values of the data, the order, and possibly
maintaining auxiliary data.

It is similar to the division in labour in Model-View-Controller.

One of the advantages is you can flip to a JComboBox with a
ComboBoxModel subclass of your ListModel. You can also use a
ComboBoxModel on a JList.

for more details see http://mindprod.com/jgloss/jlist.html
http://mindprod.com/jgloss/jcombobox.html

--
Roedy Green Canadian Mind Products
http://mindprod.com

"For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JList index 14
Can an Applet beep? 4
Official Java Classes 10
File over network timeout 3
ListModel name 10
Free keyboard applet 5
Accessing static field 21
Substring 53

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top