rachel
rachel

Reputation: 63

Using a loop to create java jbuttons

I am working on a program where I have x amount of jbuttons to be created. x is stated in a text file, and each jbutton is associated with a number, which will be displayed on the jbutton. I am assuming the best way to do this would be with a loop, but I don't really understand how to do this. Can I append the jbutton name with the number the that is associated with the jbutton, so that each jbutton has a distinct name? Can anyone explain how to do this for me?

Upvotes: 0

Views: 1415

Answers (1)

aryn.galadar
aryn.galadar

Reputation: 736

Probably the best way to do that would be to use an array (if you know how many before creating them) or a List.

Something like:

List<JButton> buttons = new ArrayList<JButton>();
while (haveMoreButtonsToCreate) {
  buttons.add(new JButton());
}

Upvotes: 2

Related Questions