0

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?

2
  • 1
    What have you tried? Commented Apr 24, 2013 at 21:40
  • You need to divide your problem... First at all, looking for how to read txt file... Then when you know how to do that, you may try to Google JButtons loops. Basically that's it, but you need to come here you more specific questions. Commented Apr 24, 2013 at 21:43

1 Answer 1

2

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());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just add a counter to the above code and increment it in the loop. Then name the button based on the value of the counter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.