• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick Java Question

I'm writing an applet that involves 8 different shipping zones. The user will select the zone through a Checkbox. I think an array could be useful, but I'd like to construct each Checkbox at the same time. How should I go about doing that?

Checkbox[] shipZone = new Checkbox

that's what I'm thinking of doing, but I don't know what goes after the end so that I can construct each checkbox with their title.

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Checkbox.html For now, I want to use the second constructor they have listed that accepts a String
 
How are you going to construct 8 checkboxes at once with 8 seperate strings? You're going to have to do something 8 times. Or you could create an array of strings, and an array of checkboxes, and do something like:

String [8] strings;

// Set up all 8 strings here

Checkbox [8] shipZone;
for(int ii = 0; ii < 8; ii++){
shipZone[ii] = new Checkbox(strings[ii]);
}
 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class shipPackage extends Applet
{

Label labTitle = new Label("Package Shipping");
Checkbox[] shipZone = new Checkbox({"",""});
public void init()
{
setBackground(Color.lightGray);
add(labTitle);
}//init
}//class

I haven't started actionlistener yet because I'm not that far, but it shouldn't affect this yet
 
Originally posted by: notfred
How are you going to construct 8 checkboxes at once with 8 seperate strings? You're going to have to do something 8 times. Or you could create an array of strings, and an array of checkboxes, and do something like:

String [8] strings;

// Set up all 8 strings here

Checkbox [8] shipZone;
for(int ii = 0; ii < 8; ii++){
shipZone[ii] = new Checkbox(strings[ii]);
}

thanks, that works, i was just checking if there was a shorter way
 
Back
Top