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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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?
0
jkteater
Asked:
jkteater
  • 11
  • 6
1 Solution
 
CEHJCommented:
Iterate the model of the JList. You would be better to hold a proper object type in the model
0
 
CEHJCommented:
What type is in the model at the moment?
0
 
jkteaterAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
That doesn't tell us what type you're storing
0
 
jkteaterAuthor Commented:
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
 
for_yanCommented:

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

Open in new window

0
 
CEHJCommented:
You could pass the whole model to the other class
0
 
jkteaterAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
The first class can then use the Iterator code i posted to get the contents of the JList
0
 
jkteaterAuthor Commented:
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
 
CEHJCommented:
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
 
jkteaterAuthor Commented:
Great Help - Thanks
0
 
CEHJCommented:
:)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now