We use cookies to collect and analyze information on site performance and usage,
to provide social media features and to enhance and customize content and advertisements.
import java.awt.*;
import java.awt.event.*;
class ChoiceEx extends Frame implements ItemListener {
Choice choice = new Choice();
TextArea textarea = new TextArea();
ChoiceEx() {
super("");
for (int i=0; i<10; i++) {
choice.addItem("item "+i);
}
// Set listeners
choice.addItemListener(this);
add(choice, BorderLayout.SOUTH);
add(textarea, BorderLayout.NORTH);
pack();
setVisible(true);
}
// When list or choice is updated
public void itemStateChanged(ItemEvent evt) {
textarea.setText("Item #" + choice.getSelectedIndex());
}
static public void main(String[] args) {
new ChoiceEx();
}
}