• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 760
  • Last Modified:

How do I sort a JLIST - Java Swing

I am new to Java Programming and I have a populated JLIST.  But when the users add a new element I want the JLIST to be sorted.  I am not finding a easy way to do it.
String[] emails = project.getEmailAddresses(); <--- getting email address from a function	    	
    	listModel = new DefaultListModel();
    	
    	
    	for (int i = 0; i < emails.length; ++i) {
         	listModel.addElement( emails[i]);      <-- adding them to the JList 	
        
        }    	    	

Open in new window


but I need to figure out how to sort the list when the user adds a new email - which is done when I Button

listModel.insertElementAt(addEmail.getText(), index);

Any help would be great
0
jkteater
Asked:
jkteater
  • 3
1 Solution
 
HonorGodSoftware EngineerCommented:
0
 
jkteaterAuthor Commented:
I have seen that article, but it seems to be a bit more than I need.  But if you think that is the only way to do the sort I will start trying to put it in my code
0
 
HonorGodSoftware EngineerCommented:
I agree that the article has provided a great deal of information, mostly (I think) to make it as general as possible.

You could probably simplify things a bit depending upon your specific needs and environment.
However, if anyone else is ever going to look at the code, it might be best to be as complete as possible.

This also allows you to use the same concept (i.e., SortedListModel class) elsewhere.

My preference is to keep a collection of utility classes available, and reuse them whenever possible.
0
 
HonorGodSoftware EngineerCommented:
Thanks for the grade & points.

Good luck & have a great day.
0
 
objectsCommented:
in your case you could just sort the array before creating your model
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now