Solved

Function to gather values of a Jlist

Posted on 2011-03-21
18
331 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
[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
  • 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
Technology Partners: 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!

 
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
 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
Java: anonymous class 4 37
dao vs facade design patterns 2 30
batch vs regular insert in spring DAO 2 20
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

740 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