Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add items to Choice

Posted on 1998-07-20
3
Medium Priority
?
304 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 80 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

971 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