Solved

How do I sort a JLIST - Java Swing

Posted on 2011-03-18
5
734 Views
Last Modified: 2012-05-11
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
Comment
Question by:jkteater
  • 3
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 35165146
0
 

Author Comment

by:jkteater
ID: 35165295
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
 
LVL 41

Accepted Solution

by:
HonorGod earned 250 total points
ID: 35165550
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 35165684
Thanks for the grade & points.

Good luck & have a great day.
0
 
LVL 92

Expert Comment

by:objects
ID: 35169401
in your case you could just sort the array before creating your model
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question