Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Choice boxes -- String Array Help

Status
Not open for further replies.

ceaton

MIS
Apr 26, 1999
27
US
Hi, I'm trying to have two string arrays (State, County) based on the first selection puts the counties into the second choice box. I'm trying to combine my classes within my applet and I'm having problems with my applet not initializing and I'm unsure where I'm going wrong. If you could please help out a very distressed programmer that would great. Here is the code, several classes at the bottom help with loading and clearing the choice boxes. My main goal is to initialize the choice boxes from within my applet not from another applet or class, I have done that. I need it done this way so that I may use the second choice box answers within my applet. <br>
<br>
Thanks in advance, <br>
<br>
MAIN APPLET <br>
<br>
import java.applet.*;<br>
import java.awt.*;<br>
import java.awt.event.*;<br>
<br>
public class state extends Applet {<br>
<br>
//subChoice sub;<br>
cub sub;<br>
Applet a; <br>
<br>
//setup the layout<br>
public void init() {<br>
setLayout(new FlowLayout());<br>
a = this;<br>
stateData sd;<br>
sd = new stateData();<br>
sub = new cub(sd.StateNames, sd.CityStateNames);<br>
add(cub);<br>
} <br>
<br>
}<br>
<br>
class cub extends Component {<br>
Applet a;<br>
String[] category; // Category Names;<br>
String[][] subCategory; // Sub-Category details<br>
Choice left; // Choice for category<br>
Choice right; // Choice for sub-category<br>
<br>
public cub (String[] category, String[][] subCategory) {<br>
this.category = category;<br>
this.subCategory = subCategory;<br>
this.a = a; <br>
<br>
//setup the left Choice<br>
left = new Choice();<br>
for (int i = 0; i &lt; category.length; i++ )<br>
left.add(category<i>);<br>
a.add(left);<br>
left.addItemListener(new leftHandler(this));<br>
updateRight(subCategory[0]);<br>
<br>
//setup the right choice<br>
//with the first (position 0) set of sub-Categories <br>
right = new Choice();<br>
a.add(right);<br>
right.addItemListener(new rightHandler(this));<br>
updateRight(subCategory[0]);<br>
}<br>
<br>
// update the right choices<br>
public void updateRight (String[] s) {<br>
right.removeAll();<br>
for (int i = 0; i &lt; s.length; i++ )<br>
right.add(s<i>);<br>
right.select(0);<br>
} <br>
<br>
}<br>
<br>
class leftHandler implements ItemListener {<br>
<br>
cub s;<br>
<br>
public leftHandler (cub s) {<br>
this.s = s;<br>
}<br>
<br>
public void itemStateChanged (ItemEvent e) {<br>
int i = s.left.getSelectedIndex();<br>
s.updateRight(s.subCategory<i>);<br>
s.a.showStatus(&quot;State = &quot; + s.left.getSelectedItem());<br>
} <br>
}<br>
<br>
class rightHandler implements ItemListener {<br>
<br>
cub s; <br>
<br>
public rightHandler (cub s) {<br>
this.s = s;<br>
}<br>
<br>
public void itemStateChanged (ItemEvent e) {<br>
s.a.showStatus(&quot;City = &quot; + s.right.getSelectedItem());<br>
<br>
} <br>
}<br>
<br>
<br>
<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Hi!<br>
<br>
I read your code (can not try, because you did not give the stateData class),and I have found problems and possible mistakes:<br>
<br>
//setup the left Choice<br>
left = new Choice();<br>
for (int i = 0; i &lt; category.length; i++)<br>
left.add(category); // possible wrong. use left.add(category<i>);<br>
<br>
// update the right choices<br>
public void updateRight (String[] s) {<br>
right.removeAll();<br>
for (int i = 0; i &lt; s.length; i++)<br>
right.add(s); // possible wrong. use right.add(s<i>);<br>
right.select(0);<br>
} <br>
<br>
I do not understand:<br>
- Why do you need a cub class (extends Component)? Why just Component? You add it to the applet, but why? It do nothing, not visible, has no function, etc.<br>
- Why do you need two ItemListeners? Use one, and check the source of the event. For example: if (left.equals(e.getSource())) { ... }.<br>
<br>
Piece of advice:<br>
- The first letter of the class names always will be capital letter.<br>
- Do not use public and etc. for constructors.<br>
<br>
I hope these things will help you.<br>
I think, that you want to do is simply, but your code is very complicated.<br>
If you send the missing class, I would gladly see the complete program.<br>
<br>
Good luck. Bye, Otto.<br>

 
Thanks, I think I have come up with a solution but I'm still working on it...thanks.<br>
<br>
Courtney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top