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

        // Define a Container holding a Question label, a List
        // of possible answers and a Submit button.
public class QuestionAsker extends Container
                        implements ActionListener, ItemListener {
    public QuestionAsker(String question,String[] possibleAnswers){
        // For simplicity, use a BorderLayout.
        setLayout(new BorderLayout());
        setBackground(Color.white);
        setFont(new Font("Dialog",Font.PLAIN,10));

        // Display the question.
        add(new Label(question),"North");

        // Store the answers in a list.
        List answers = setupAnswerList(possibleAnswers);
        answers.setBackground(Color.white);
        setAnswerList(answers);
        add(answers,"Center");

        // The button to be pressed when the answer is complete.
        Button b = new Button("Submit");
        b.addActionListener(this);
        add(b,"South");
    }

        // Implementation of ActionListener.
        // Respond when the submit button is pressed.
    public void actionPerformed(ActionEvent e){
        // Get the final list of selections.
        List list = getAnswerList();
        String[] answers = list.getSelectedItems();
        if((answers == null) || (answers.length == 0)){
            System.out.println("There were no answers selected.");
        }
        else{
            System.out.print("The answers selected were: ");
            for(int i = 0; i < answers.length; i++){
                System.out.print(answers[i]+" ");
            }
            System.out.println();
        }
        System.exit(0);
    }

        // Implementation of ItemListener.
        // Monitor when an item is selected or deselected.
    public void itemStateChanged(ItemEvent e){
        // Obtain information about which item has been (de)selected.
        Object o = e.getItem();
        if((o != null) && (o instanceof Integer)){
            // The index of the item.
            int index = ((Integer) o).intValue();
            int stateChange = e.getStateChange();
            if((stateChange & ItemEvent.SELECTED) ==
                                                ItemEvent.SELECTED){
                System.out.println("Item "+index+" selected.");
            }
            else if((stateChange & ItemEvent.DESELECTED) ==
                                                ItemEvent.DESELECTED){
                System.out.println("Item "+index+" deselected.");
            }
            else{
                System.err.println(
                        "Unidentified state change to item: "+index);
            }
        }
        else{
            System.err.println("Unknown item event: "+e+" on "+o);
        }
    }

        // Setup the List from an array of possible answer strings.
    protected List setupAnswerList(String[] possibleAnswers){
        // Create the List to contain the possible answers.
        // Allow one row per answer.
        // Allow multiple selected items.
        List answers = new List(possibleAnswers.length,true);
        // Add the possibleAnswers to the list.
        for(int i = 0; i < possibleAnswers.length; i++){
            answers.add(possibleAnswers[i]);
        }
        // Notify this object of changes to the list.
        answers.addItemListener(this);
        return answers;
    }

    protected List getAnswerList(){
        return answerList;
    }

    protected void setAnswerList(List list){
        answerList = list;
    }

    private List answerList;
}
