Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to add items to Choice

Posted on 1998-07-20
3
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 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