• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Help Please: Case Statements and ItemEvents

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
johnome
Asked:
johnome
  • 2
  • 2
1 Solution
 
objectsCommented:
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
 
johnomeAuthor Commented:
Thank you very much indeed
0
 
CEHJCommented:
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
 
CEHJCommented:
! (always check before posting!)
0
 
objectsCommented:
:-)
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now