0

I have the following custom class for my JList:

package ericsonwrp.republica.vintage.caixa;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractListModel;

@SuppressWarnings("hiding")
class SortedListModel<Object> extends AbstractListModel<Object> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    SortedSet<Object> model;

    public SortedListModel() {
        model = new TreeSet<Object>();
    }

    public int getSize() {
        return model.size();
    }

    @SuppressWarnings("unchecked")
    public Object getElementAt(int index) {
        return (Object) model.toArray()[index];
    }

    public void addElement(Object element) {
        if (model.add(element)) {
            fireContentsChanged(this, 0, getSize());
        }
    }

    public void addAll(Object elements[]) {
        Collection<Object> c = Arrays.asList(elements);
        model.addAll(c);
        fireContentsChanged(this, 0, getSize());
    }

    public void removeAllElements() {
        model.clear();
        fireContentsChanged(this, 0, getSize());
    }

    public boolean contains(Object element) {
        return model.contains(element);
    }

    public Object firstElement() {
        return model.first();
    }

    public Iterator<Object> iterator() {
        return model.iterator();
    }

    public Object lastElement() {
        return model.last();
    }

    public boolean removeElement(Object element) {
        boolean removed = model.remove(element);
        if (removed) {
            fireContentsChanged(this, 0, getSize());
        }
        return removed;
    }
}

The SortedSet does not sort as it was supposed to, as you can see:

enter image description here

It goes from 0 to 1 and then to 10 instead of 2. How can I sort the ListModel in ascending order?

Ericson Willians
  • 7,606
  • 11
  • 63
  • 114
  • How do you sort **Objects**? Which one is greater "new Object()" or "new Object()"?! Meaning: if you want to do meaningful sorting, why are you using a class on which that doesnt make any sense in the first place?! – GhostCat Oct 04 '16 at 08:40
  • See [*Creating a Sorted Component*](http://www.oracle.com/us/technologies/java/sorted-jlist-136883.html), for [example](http://stackoverflow.com/a/37723271/230513). – trashgod Oct 04 '16 at 08:42
  • `The SortedSet does not sort as it was supposed to, as you can see:` - actually I can't see. The image is to small. When you post a picture post the relevant image of the frame. The whole desktop is irrelevant to the question. – camickr Oct 04 '16 at 14:06
  • You just need to click on it. – Ericson Willians Oct 04 '16 at 14:17

0 Answers0