Solved

How do I sort a JLIST - Java Swing

Posted on 2011-03-18
5
742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 106
Why is enum singleton a better approach than static factory 3 67
collection output issue 9 96
ArrayList Adding/settign data 3 38
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

710 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