Solved

Function to gather values of a Jlist

Posted on 2011-03-21
18
321 Views
Last Modified: 2012-05-11
I have a class file named Projects that has a jbutton called emails.  When the user clicks the button it pops up a Email dialog window.  The Email dialog is also a separate class file.  In the Email dialog there is a populated list of emails and the user has the option to add or remove emails from the list.  I am using a JList to store the emails.  

What I am needing is a function that when the user clicks the OK button on the Email Dialog.  It will get all the values of the Jlist and push it to the Projects class file.  I am not sure how to do that?
0
Comment
Question by:jkteater
  • 11
  • 6
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180522
Iterate the model of the JList. You would be better to hold a proper object type in the model
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180525
What type is in the model at the moment?
0
 

Author Comment

by:jkteater
ID: 35180683
This is the model I am using.  Any help you can give me would be great.  I am very new at JAVA / Swing / UI stuff.  I am still trying to figure out how all this works.
final SortingListModel model = new SortingListModel();

public class SortingListModel extends AbstractListModel
    {
    	TreeSet model;
        
    	private Comparator USEFUL_COMPARATOR = new Comparator()
            {
                public int compare( Object o1, Object o2 )
                {
                    String str1 = o1.toString();
                    String str2 = o2.toString();
                    Collator collator = Collator.getInstance();
                    int result = collator.compare( str1, str2 );
                    return result;
                }
            };

    public SortingListModel()
    {
        model = new TreeSet( USEFUL_COMPARATOR );
    }

    // ListModel methods
    public int getSize()
    {
        // Return the model size
        return model.size();
    }

    public Object getElementAt( int index )
    {
        // Return the appropriate element
        return model.toArray()[index];
    }

    // Other methods
    public void addElement( Object element )
    {
        
        if( model.add( element ))
        {
            fireContentsChanged( this, 0, getSize() );
        }
    }

    public void addAll( Object elements[] )
    {
        Collection c = Arrays.asList(elements);
        model.addAll(c);
        fireContentsChanged( this, 0, getSize() );
    }

    public void clear()
    {
        model.clear();
        fireContentsChanged( this, 0, getSize() );
    }

    public boolean contains( Object element )
    {
        return model.contains( element );
    }

    public Object firstElement()
    {
        // Return the appropriate element
        return model.first();
    }

    public Iterator iterator()
    {
        return model.iterator();
    }

    public Object lastElement()
    {
        // Return the appropriate element
        return model.last();
    }

    public boolean removeElement( Object element )
    {
        boolean removed = model.remove( element );
        if( removed )
        {
            fireContentsChanged( this, 0, getSize() );
        }
        return removed;
    }

 }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180704
That doesn't tell us what type you're storing
0
 

Author Comment

by:jkteater
ID: 35180725
Sorry - Is this what are needing to know?
String[] emails = project.getEmailAddresses();

for (int i = 0; i < emails.length; ++i) {
         	     	
              model.addElement(emails[i]);      
        }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35180726

There is an example of removing and adding items in the JList in this link:
http://download.oracle.com/javase/tutorial/uiswing/components/list.html#mutable

(go to the section "Adding Items to and Removing Items from a List")
It even shows how to add it in different loaction within the list;
let us know if you have questions.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180742
Looks like you're just storing String - looks OK
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180762
You just need to call iterator() on your model
0
 

Author Comment

by:jkteater
ID: 35180779
But how would I pass the value of the jlist to a different function - would I have to create a array within the new function?  
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Expert Comment

by:CEHJ
ID: 35180805
e.g.
Iterator<String> i = model.iterator();
	    while(i.hasNext()) {
		System.out.println(i.next());
	    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180808
You could pass the whole model to the other class
0
 

Author Comment

by:jkteater
ID: 35180929
Sorry for asking so many simple questions - I am just trying to understand to whole thing.
I am sure this is very simple and I am making it harder than it needs to be.

So basically in the new function for my OK button.  I assumed that when the button was clicked the function (ListGather) would get the values of the Jlist and store them in a array or something like that and then somehow pass the array to the first class.  So if I use the Iterator in the function instead of printing it out should I bee storing it in something?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 35180958
If all you want to do is to have the 'first class' do something with the values in the JList, just pass the whole ListModel to the first class
public void actionPerformed(ActionEvent e) {
    // Do it
    firstClass.fillIt(model);
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35180981
The first class can then use the Iterator code i posted to get the contents of the JList
0
 

Author Comment

by:jkteater
ID: 35181021
That does sound like what I am looking for.  Let me give it a try and get back - Thanks for hanging with me, I have a feeling I will be posting a lot of questions and this has been a great start.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35181056
OK. The point  is - since you already have a collection, there's no need to create another container class (unless for some reason you didn't trust the code in the 'first class')
0
 

Author Closing Comment

by:jkteater
ID: 35181258
Great Help - Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35181350
:)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
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…
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 video teaches viewers about errors in exception handling.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now