Solved

How do I sort a JLIST - Java Swing

Posted on 2011-03-18
5
737 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
servlet doXXX methods 3 62
arguments to jar 5 35
printf performancy 11 71
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

829 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