Solved

Function to gather values of a Jlist

Posted on 2011-03-21
18
333 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 91
SHA2 certs for IIS AND Java? 2 136
Desingning Refactoring existing code 2 46
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 73
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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