Results 1 to 2 of 2
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default I used a TreeSet model to get a sorted JList but it isn't sorted.

    I implemented an AbstractListModel using a sorted set in order to make a JList, but when i run the program, the list is unsorted. What am I doing wrong?

    SortedListModel.java
    Java Code:
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.SortedSet;
    import java.util.TreeSet;
    import java.util.Arrays;
    
    import javax.swing.AbstractListModel;
    
    
    public class SortedListModel extends AbstractListModel {
    
    	SortedSet listModel;
    	
    	public SortedListModel()
    	{
    		listModel = new TreeSet();
    	}
    	
    	public Object getElementAt(int index) {
    		return listModel.toArray()[index];
    	}
    	
    	public int getSize() {
    		return listModel.size();
    	}
    	
    	public void addAll(Object elements[])
    	{
    		Collection c  = Arrays.asList(elements);
    		listModel.addAll(c);
    		fireContentsChanged(this,0,getSize());
    	}
    	
    	public void clear()
    	{
    		listModel.clear();
    		fireContentsChanged(this,0,getSize());
    	}
    	
    	public boolean contains(Object element)
    	{
    		return listModel.contains(element);
    	}
    	
    	public Object firstElement()
    	{
    		return listModel.first();
    	}
    	
    	public Object lastElement()
    	{
    		return listModel.last();
    	}
    	
    	public Iterator iterator()
    	{
    		return listModel.iterator();
    	}
    	
    	public boolean removeElement(Object element)
    	{
    		boolean removed = listModel.remove(element);
    		if(removed)
    		{
    			fireContentsChanged(this,0,getSize());
    		}
    		return removed;
    	}
    
    }
    ContactList.java
    Java Code:
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.List;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JTextField;
    import javax.swing.ListSelectionModel;
    
    
    public class ContactList {
    	private JFrame window;
    	private JList contactList;
    	private ArrayList<Person> people;
    	private JTextField nameField;
    	private JTextField ageField;
    	private JButton addNewPerson;
    	
    	public ContactList()
    	{
    		createContactList();
    		createUI();
    	}
    	
    	private void createUI()
    	{
    		window = new JFrame();
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setVisible(true);
    		window.setSize(new Dimension(300,500));
    		
    		SortedListModel model = new SortedListModel();
    		contactList = new JList(model);
    		updateList();
    		contactList.setVisibleRowCount(4);
    		
    		nameField = new JTextField(10);
    		
    		ageField = new JTextField(3);
    		
    		addNewPerson = new JButton("+");
    		addNewPerson.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent event)
    			{
    				Person p = new Person(nameField.getText(),Integer.parseInt(ageField.getText()));
    				people.add(p);
    				updateList();
    			}
    		});
    		
    		
    		Container panel = window.getContentPane();
    		panel.setLayout(new FlowLayout());
    		panel.add(contactList);
    		panel.add(nameField);
    		panel.add(ageField);
    		panel.add(addNewPerson);
    	}
    	
    	private void updateList()
    	{
    		contactList.setListData(people.toArray());
    	}
    	
    	private void createContactList()
    	{
    		people = new ArrayList<Person>();
    		
    		Person p1 = new Person("Fred",55);
    		Person p2 = new Person("Ben", 24);
    		Person p3 = new Person("Will", 49);
    		Person p4 = new Person("Sally", 21);
    		Person p5 = new Person("Sally", 21);
    		
    		people.add(p4);
    		people.add(p3);
    		people.add(p2);
    		people.add(p1);
    		people.add(p5);
    	}
    	
    	public static void main(String args[])
    	{
    		new ContactList();
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    You should be putting your data into the Model not the JList.
    setListData() creates a new Model for the supplied data, as per the API.

Similar Threads

  1. Why is my ArrayList not sorted?
    By Tabula Rasa in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 09:31 PM
  2. Checking if an ArrayList is sorted
    By MonkeyGrad in forum New To Java
    Replies: 4
    Last Post: 04-13-2011, 07:27 AM
  3. Casting an int from a sorted set
    By Bmack in forum New To Java
    Replies: 2
    Last Post: 03-17-2010, 08:09 PM
  4. Sorted LinkList problem
    By koolaqua16 in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2009, 07:49 AM
  5. My doublyLinked list does not get sorted
    By hasani6leap in forum New To Java
    Replies: 0
    Last Post: 01-06-2008, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •