Question

I'm trying to use the swingx library to use a JXList, which supports sorting.

I'm using the sorting methods provided in the API documentation but it still doesn't work.

Here's a full (test) example in which I would like A, B, and C to be sorted in ascending order.

import java.awt.Dimension;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.jdesktop.swingx.JXList;
import org.jdesktop.swingx.decorator.SortOrder;

public class TestWin {
    public static void main(String[] args) {
        JFrame frame = new JFrame("TEST");
        JPanel panel = new JPanel();
        JXList list = new JXList();
        DefaultListModel dtm = new DefaultListModel();
        dtm.addElement("A");
        dtm.addElement("C");
        dtm.addElement("B");
        list.setModel(dtm);
        list.setSortOrder(SortOrder.ASCENDING);
        list.toggleSortOrder();
        panel.add(list);
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));
        frame.setMinimumSize(new Dimension(200, 200));
        frame.add(panel);
        frame.setVisible(true);
    }
}

However, that code doesn't work.

What am I doing wrong? Any alternative suggestions?

Thanks in advance?

Was it helpful?

Solution

You forgot

    list.setAutoCreateRowSorter(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top