Solved

How to add items to Choice

Posted on 1998-07-20
3
293 Views
Last Modified: 2010-03-30
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".)
0
Comment
Question by:AmyS
3 Comments
 

Author Comment

by:AmyS
ID: 1227199
Edited text of question
0
 

Expert Comment

by:HoJu
ID: 1227200
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+");
         }
      }
   }
}
0
 
LVL 1

Accepted Solution

by:
dryang earned 20 total points
ID: 1227201
this is a full working program which shows the logic needed for your question. This is the class which handles the logic. The main class is below this:

// class to implement logic for different choices
//
//
// 22nd July, Copyright Wong Ruey Yang"dryang"
// Ngee Ann Polytechnic, Singapore
//

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

public class ChoiceLogic extends Frame
      implements ActionListener,ItemListener,WindowListener
{
      Choice ChoiceA;
      Choice ChoiceB;
      
      Button toggle;
      
      boolean situation = true;
      
      Panel pnl;
      
      //constructor
      
      //init GUI
      ChoiceLogic()
      {
            setSize(400,100);
            addWindowListener(this);
            
            pnl = new Panel();
            
            toggle = new Button("Toggle Situation");
            toggle.setBounds(new Rectangle(100,25));
            
            ChoiceA = new Choice();
            ChoiceA.setBounds(new Rectangle(150,25));
            ChoiceA.addItemListener(this);
            
            ChoiceB = new Choice();
            ChoiceB.setBounds(new Rectangle(150,25));
            
            toggle.addActionListener(this);
            
            pnl.add(toggle);
            pnl.add(ChoiceA);
            pnl.add(ChoiceB);
            
            ChoiceA.add("Green");
            ChoiceA.add("Blue");
            
            ChoiceB.add("M-18");
            ChoiceB.add("F-20");
            ChoiceB.add("F45+");
            
            add(pnl);
            setVisible(true);
            
            validate();
            }
            
            public void actionPerformed(ActionEvent AE)
            {
                  if(AE.getSource() == toggle)
                  {
                        if(situation)
                        {
                              ChoiceA.removeAll();
                              ChoiceA.add("Green X");
                              ChoiceA.add("Green Y");
                              ChoiceA.add("Blue");
                              
                              ChoiceB.removeAll();
                              ChoiceB.add("M-18");
                              ChoiceB.add("F 45");
                              
                              situation = false;
                              
                              }
                        else if(!situation)
                        {
                              ChoiceA.removeAll();
                              ChoiceA.add("Green");
                              ChoiceA.add("Blue");
            
                              ChoiceB.removeAll();            
                              ChoiceB.add("M-18");
                              ChoiceB.add("F-20");
                              ChoiceB.add("F45+");
                              
                              situation = true;
                              }
                        }
                  }//actionPerformed Last bracket
            
            public void itemStateChanged(ItemEvent IE)
            {
                  if(IE.getSource() == ChoiceA)
                  {
                        if(ChoiceA.getSelectedItem().equals("Green"))
                        {
                              ChoiceB.removeAll();
                              ChoiceB.add("M-18");
                              ChoiceB.add("F-20");
                              ChoiceB.add("F45+");
                              }
                        else if(ChoiceA.getSelectedItem().equals("Blue"))
                        {
                              ChoiceB.removeAll();
                              ChoiceB.add("M21+");
                        }
                        else if(ChoiceA.getSelectedItem().equals("Green X"))
                        {
                              ChoiceB.removeAll();
                              ChoiceB.add("M-18");
                              ChoiceB.add("F45");
                              }
                        else if(ChoiceA.getSelectedItem().equals("Green Y"))
                        {
                              ChoiceB.removeAll();
                              ChoiceB.add("F20");
                              }
                        
                  }//
                        
                  }//itemStateChange last Bracket

      private void cancel()
      {
            dispose();
            System.exit(0);
            }
      
      public void windowActivated(WindowEvent WE){}
      public void windowClosed(WindowEvent WE){}
      public void windowClosing(WindowEvent WE){cancel();}
      public void windowDeactivated(WindowEvent WE){}
      public void windowDeiconified(WindowEvent WE){}
      public void windowIconified(WindowEvent WE){}
      public void windowOpened(WindowEvent WE){}            
            
            
      }//end of class


The main class to launch the program

public class ChoiceIt
{
      public static void main(String args[])
      {
            ChoiceLogic cl = new ChoiceLogic();
            }
      }

complie both and type "java ChoiceIt"
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now