Share this page 

Synchronize a TextArea vs a ChoiceTag(s): AWT


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();
    }
}