Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I sort a JLIST - Java Swing

Posted on 2011-03-18
5
Medium Priority
?
751 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 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

824 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