Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag for United States of America

asked on

Function to gather values of a Jlist

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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Iterate the model of the JList. You would be better to hold a proper object type in the model
What type is in the model at the moment?
Avatar of jkteater

ASKER

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

That doesn't tell us what type you're storing
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


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.

Looks like you're just storing String - looks OK
You just need to call iterator() on your model
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?  
e.g.
Iterator<String> i = model.iterator();
	    while(i.hasNext()) {
		System.out.println(i.next());
	    }

Open in new window

You could pass the whole model to the other class
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?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The first class can then use the Iterator code i posted to get the contents of the JList
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.
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')
Great Help - Thanks
:)