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

[714 byte] By [Triptronicxa] at [2007-10-2 16:45:39]
# 1

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.

atmguya at 2007-7-13 17:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 2
Excellent thanks, this looks something like what I am looking for.I will give it a try and see what happens
Triptronicxa at 2007-7-13 17:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

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

Triptronicxa at 2007-7-13 17:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

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]);

}

}

atmguya at 2007-7-13 17:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 5
Cool, thanks.I did try ths, I must have done something a bit odd with it, will give it another crack.trip
Triptronicxa at 2007-7-13 17:55:54 > top of Java-index,Desktop,Core GUI APIs...