Solved

How to add items to Choice

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now