Solved

How to add items to Choice

Posted on 1998-07-20
3
297 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

821 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