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?
-
1What have you tried?Bohemian– Bohemian ♦2013-04-24 21:40:21 +00:00Commented 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.Castiblanco– Castiblanco2013-04-24 21:43:54 +00:00Commented Apr 24, 2013 at 21:43
Add a comment
|
1 Answer
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());
}
1 Comment
CBass
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.