We help IT Professionals succeed at work.

itemListener

YamSeng
YamSeng asked
on
In my java applet, I have a list which I want to capture the selections.  I am using AWT and I think I have to use itemListener to capture it.

Can anyone show me an example?  say if I've got a list which looks like this

 List lst = new List(4, false);
 lst.add("Mercury");
 lst.add("Venus");
 lst.add("Earth");
 lst.add("JavaSoft");
 lst.add("Mars");
 lst.add("Jupiter");
 lst.add("Saturn");
 lst.add("Uranus");
 lst.add("Neptune");
 lst.add("Pluto");
 cnt.add(lst);
 
So how can I get the itemlistener to work and know what is being selected?
Comment
Watch Question

This is an example:

package aa;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MyApplet extends Applet {

    /**Construct the applet*/
    public MyApplet() {
    }

    public void init() {
        List l = new List(3);
        l.addItemListener(new MyListener());
        l.add("AAA");
        l.add("BBB");
        l.add("CCC");
        add("My list", l);
    }


    private class MyListener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("item: " + e.getItem());
            int state = e.getStateChange();
            if (state == e.SELECTED) {
                System.out.println("selected...");
            } else {
                System.out.println("deselected...");            }
        }
    }
}
In the example the item listener is an inner class. You could make it a separate class if you intend to use it often.
Or, you could define it as anonymous class:

package aa;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MyApplet extends Applet {

    /**Construct the applet*/
    public MyApplet() {
    }

    public void init() {
        List l = new List(3);
        l.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    System.out.println("item: " + e.getItem());
                    int state = e.getStateChange();
                    if (state == e.SELECTED) {
                        System.out.println("selected...");
                    } else {
                        System.out.println("deselected...");            }
                    }
                });
        l.add("AAA");
        l.add("BBB");
        l.add("CCC");
        add("My list", l);
    }
}

Using the received ItemEvent you could find out what the concrete state change was and what the changed item was.

Author

Commented:
how I can know which value in the list is selected?  ie, how to get the string of the value selected? AAA or BBB?
ItemEvent.getItem returns the number of the item. Then you can get the String value by the index:

package aa;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MyApplet extends Applet {

    private List l;

   /**Construct the applet*/
   public MyApplet() {
   }

   public void init() {
       l = new List(3);
       l.addItemListener(new MyListener());
       l.add("AAA");
       l.add("BBB");
       l.add("CCC");
       add("My list", l);
   }


   private class MyListener implements ItemListener {
       public void itemStateChanged(ItemEvent e) {
           System.out.println("item: " + e.getItem());

            System.out.println(l.getItem(((Integer) e.getItem()).intValue()));

           int state = e.getStateChange();
           if (state == e.SELECTED) {
               System.out.println("selected...");
           } else {
               System.out.println("deselected...");            }
       }
   }
}

Author

Commented:
if I have the index number returned, will I be able to access the string using any of the list's method?

Or do I have to keep an array of the strings I've used in adding to the list?
Yes, you can get the string value from the list - as I've shown in the last example:

l.getItem(((Integer) e.getItem()).intValue());

(Using the getItem(int index) method of the List)
Do you need more help?

Author

Commented:
thanks.  Got it.

I misinterpreted that line...I thought it was a typo   8)

Author

Commented:
thanks.  Got it.

I misinterpreted that line...I thought it was a typo   8)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.