0

How would I go about creating lots of JButtons and JLables without having to declare a long list of them , I'm not sure how to do it if I made a for loop which said e.g. "for (int I = 0; I < 30; I++) { JButton button = new JButton(); } Although this would create many buttons I want to be able to individually edit each button later on so how would I do this loop of buttons but them all having different names. Any help is very much appreciated :)

Josh Nolan
  • 11
  • 2
  • 1
    If this is not a duplicate, please [edit] your question to include a [mre] that shows your revised approach. – trashgod Mar 13 '22 at 22:51
  • also note: stick with java naming conventions, variable/field names start lower case, so it would be `for (int i` ... and understand: there are different ways to go about this. For example: when you are able to write you code so that you can give the corresponding ActionListener to your button right when you create that button ... then there might be no need to "remember" the different JButton instances in code of yours. – GhostCat Mar 14 '22 at 15:26
  • You need some way to identify the created `JButton`s. There are a few ways to do this. One is to use the `JButton`s text - as indicated in the third duplicate question. Other ways include setting the _name_ property by calling method `setName` or the _actionCommand_ property by calling method `setActionCommand` or setting a custom property by calling method `putClientProperty`. – Abra Mar 19 '22 at 08:51

0 Answers0