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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.getIt em(((Integ er) e.getItem()).intValue()));
int state = e.getStateChange();
if (state == e.SELECTED) {
System.out.println("select ed...");
} else {
System.out.println("desele cted..."); }
}
}
}
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
System.out.println("item: " + e.getItem());
System.out.println(l.getIt
int state = e.getStateChange();
if (state == e.SELECTED) {
System.out.println("select
} else {
System.out.println("desele
}
}
}
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?
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)
l.getItem(((Integer) e.getItem()).intValue());
(Using the getItem(int index) method of the List)
Do you need more help?
ASKER
thanks. Got it.
I misinterpreted that line...I thought it was a typo 8)
I misinterpreted that line...I thought it was a typo 8)
ASKER
thanks. Got it.
I misinterpreted that line...I thought it was a typo 8)
I misinterpreted that line...I thought it was a typo 8)
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
System.out.println("item: " + e.getItem());
int state = e.getStateChange();
if (state == e.SELECTED) {
System.out.println("select
} else {
System.out.println("desele
}
}
}