?
Solved

Help Please: Case Statements and ItemEvents

Posted on 2003-02-26
5
Medium Priority
?
209 Views
Last Modified: 2010-03-31
Hi,
I was wondering if anyone could help me with this or guide me in the right direction. What I am trying to do is to build an applet that takes user information, loads different data and then compiles a strategy based on this data. The code following is my itemStateChanged() method.

What I want to do is that if the user chooses track, which is a Choice(); with 7 items, if the user chooses the first item for track, it does something, else if the user chooses second item for track it does something and so on. This is better explained in the commented code.
Thanks for your time.

public void itemStateChanged(ItemEvent ie)
{
 if (ie.getSource().equals((Object)track))
 {
  //if(item 1 is selected from track)
  //{
  //  Lap = 53;
  //  Distance = 5.303;
  //}
  //else if(item 2 is selected from track)
  //{
  //  Lap = 62;
  //  Distance = 4.207;
  //}
  //else
  //{
  //  Lap = 0;
  //  Distance = 0;
  //}

  trackLabelb.setText(track.getSelectedItem());
 }
 else if(ie.getSource().equals((Object)weather))      
 {
  weatherLabelb.setText(weather.getSelectedItem());
 }
 else if(ie.getSource().equals((Object)tyre))  
 {
  tyreLabelb.setText(tyre.getSelectedItem()); //and so on
 }
 else if(ie.getSource().equals((Object)safetyCar))  
 {
  safetyLabelb.setText(safetyCar.getSelectedItem());
 }
  else if(ie.getSource().equals((Object)yellows))
 {
  yellowLabelb.setText(yellows.getSelectedItem());      
 }
 else if(ie.getSource().equals((Object)reds))  
 {
  redLabelb.setText(reds.getSelectedItem());
 }
 else
 {
  Checkbox cb=(Checkbox)ie.getItemSelectable();
  if (cb.getLabel().equals("Frontwing"))
  {
    frontLabelb.setText((cb.getState())?"true":"false");
  }
  else if (cb.getLabel().equals("Rearwing"))
  {
     rearLabelb.setText((cb.getState())?"true":"false");
  }
  else if (cb.getLabel().equals("Tyre Damage"))
  {
    wheelLabelb.setText((cb.getState())?"true":"false");
  }
  else if (cb.getLabel().equals("Drive through Penalty"))
  {
    driveLabelb.setText((cb.getState())?"true":"false");
  }
  else if(cb.getLabel().equals("Stop Go Penalty"))
  {
    stopLabelb.setText((cb.getState())?"true":"false");
  }
  else
  {
     optionLabelb.setText(cb.getLabel());
  }
}
}
0
Comment
Question by:johnome
[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
  • 2
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 8027119
if (ie.getSource().equals((Object)track))
{
 switch (track.getSelectedIndex())
 {
   case 0:
     Lap = 53;
     Distance = 5.303;
     break;
   case 1:
     Lap = 62;
     Distance = 4.207;
     break;
 }
}
...

 
0
 

Author Comment

by:johnome
ID: 8027150
Thank you very much indeed
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8027160
The way i'd implement this is to use the selected index of the choice as an index into a LinkedList of 'CircuitInfo' objects:


class CircuitInfo {
  int lap;
  double distance;
 
  public CircuitInfo(int lap, double distance) {
    this.lap = lap;
    this.distance = distance;
  }
}

LinkedList circuitInfo = new LinkedList();
circuitInfo c0 = new CircuitInfo(0, 0);
circuitInfo.add(c0);
circuitInfo c1 = new CircuitInfo(53, 5.303);
circuitInfo.add(c1);

Got the idea?


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8027169
! (always check before posting!)
0
 
LVL 92

Expert Comment

by:objects
ID: 8027199
:-)
0

Featured Post

Technology Partners: 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 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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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