Link to home
Start Free TrialLog in
Avatar of YamSeng
YamSeng

asked on

itemListener

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?
Avatar of ia_ia_ia_1
ia_ia_ia_1

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...");            }
        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of ia_ia_ia_1
ia_ia_ia_1

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
Avatar of YamSeng

ASKER

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...");            }
       }
   }
}
Avatar of YamSeng

ASKER

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?
Avatar of YamSeng

ASKER

thanks.  Got it.

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

ASKER

thanks.  Got it.

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