import javax.swing.*; /* SortedListModel.java replaces the default list model with one * that keeps the list is sorted order. * * Written by: Stuart Hansen * September 8, 2008 */ public class SortedListModel extends DefaultListModel { // We override addElement in the DefaultListModel class // so that the JList remains sorted. // Note that in a more complete application, add() // set() and setElementAt() should also be overridden. public void addElement(Object obj) { String str = obj.toString(); int index = getSize(); while (index > 0 && ((String)elementAt(index-1)).compareTo(str) >=0) { index--; } super.add(index, obj); } }