Link to home
Start Free TrialLog in
Avatar of AmyS
AmyS

asked on

How to add items to Choice

In the following situation , how to add the items to the Choice:  
Choice B to be made depending on the Choice A which has made.
For example (Age and Course Categories):

1. Choice A(" Course ") has two items: Green,  Blue.
               If choice Green , Choice B ("Age categories" ) has three items: M-18, F-20, F45+.
               If choice Blue, Choice B has one item: M-21+.

2. On occasion, a competition has enough competitors to warrant separating the Green        
               course into two different courses. In this case:
             
               Choice A( Course ) has three items: GreenX, GreenY, Blue.
               If choice GreenX , Choice B has items: M-18, F45+.
               If choice GreenY, Choice B has one item:  F-20.
               If choice Blue, Choice B has one item: M-21+.

Could you please give me the correct codes for this example? (In face, there are more items in "Course" and "Age category".)
Avatar of AmyS
AmyS

ASKER

Edited text of question
Caveat 1: Doing this relies on an understanding of the Java event model -- which, from your question, I can't tell if you've learned yet. If you haven't, some of the code below might seem a bit nonsensical, but if you want to try to solve this quickly, maybe it'll help.

Caveat 2: I was up late last night, and I'm still not totally awake, so stupid errors may creep into the code below. My apologies in advance.

At any rate, hopefully this'll give you a rough idea of what's necessary, or at least it'll give us something more specific to frame your questions with.

public class MyJavaApplet extends Applet implements ItemListener
{
   private Choice   choiceA, choiceB;

   public void init()
   {
      setLayout(new FlowLayout());
      choiceA = new Choice();
      choiceA.add("Green");
      choiceA.add("Blue");
      add(choiceA);
      choiceB = new Choice();
      add(choiceB);
      choiceA.addItemListener(this);
   }

   public void itemStateChanged(ItemEvent ie)
   {
      if (ie.getStateChange() == ItemEvent.SELECTED)
      {
         choiceB.removeAll();
         if (choiceA.getSelectedItem() == "Green")
         {
            choiceB.add("M-18");
            choiceB.add("F-20");
            choiceB.add("F45+");
         }
         else
         {
            choiceB.add("M-21+");
         }
      }
   }
}
ASKER CERTIFIED SOLUTION
Avatar of dryang
dryang

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