I have the following demo app which displays a list of values, but i need to be able to control the ordering of the elements in the list. Whats the easiest way to add this?
package demo;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListModel;
public class JListOrder {
public JListOrder()
{
JFrame frame = new JFrame("JListOrder");
DefaultListModel model = new DefaultListModel();
model.addElement("z");
model.addElement("Z");
model.addElement("a");
model.addElement("A");
model.addElement("C");
model.addElement("c");
model.addElement("b");
model.addElement("B");
JList list = new JList(model);
frame.add(list);
frame.setSize(200,200);
frame.setVisible(true);
}
public static void main(String[] args) {
new JListOrder();
}
}
The easiest way is to use SwingX which supports sorting/filtering of JXList the same way as core table sorting/filtering:
JXList list = new JXList(model);
list.setAutoCreateRowSorter(true);
list.toggleSortOrder();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With