Java Applet - variable Button name
Hi,
I need to be able to recursively create buttons in an applet.
The problem I am having is being able to specify a variable name for a button... can anyone help?
e.g.
Button button1 = new Button("Button1");
Button button2 = new Button("Button2");
Button button3 = new Button("Button3");
add(button1);
add(button2);
add(button3);
... How can I do this without specifying the "button1, Button2, Button3"
because I am going to using many buttons I don't really want to be typing add(buttonX); everytime.
Sorry if this is a bit vague, let me know if you need more info for what I am trying to achieve.
cheers in advance
Trip
You can use an array of Buttons (untested code follows)Button[] buttons = new Button[NUMBER_OF_BUTTONS];
for(int i = 0;i< buttons.length;i++) {
buttons[i] = new Button("Button"+Integer.toString(i));
}
Hopefully, you are going to use button labels that make sense to the user.
public void makeButton() {
Button[] buttons = new Button[100];
for(int i = 0;i< buttons.length;i++) {
buttons[i] = new Button("Button"+Integer.toString(i));
}
}
Ok I have tried out this code, how can I add buttons to the applet?
thanks
public void makeButton() {
Button[] buttons = new Button[100];
for(int i = 0;i< buttons.length;i++) {
buttons[i] = new buttons[i] = new Button("Button"+Integer.toString(i));
add(buttons[i]);
}
}