0

I'd like to make a grid in a JPanel full of buttons, then i want to create them in a loop because i've aproximatly 100 buttons. Each button created must have an id differents see my picture to understand better :s

I think I have to create a method like this but I don't know how to proceed :

public Button(String text , int id){
     //code 
}

And then add it to a loop to create the grid :)

enter image description here

if anyone can help me it would be very appreciated ! ;)

Daahrien
  • 10,190
  • 6
  • 39
  • 71
Clyx
  • 11
  • 6
  • How do you think you might do it? You even put "loops" in the tags... – Tim B Jan 12 '14 at 09:57
  • whatever your problem (is it the loop? is it the id? is it the layout? is it ...?): tackle it one by one, best with the help of your textbook (it's **your** homework, after all, nobody here will do it for it ;-) – kleopatra Jan 12 '14 at 09:57
  • my problem is the id and the layout , and sorry for tags @Tim B – Clyx Jan 12 '14 at 09:59
  • I wasn't complaining about the tag. I was pointing out that the tag already contains half the answer you need. You are unlikely to get much help on this unless you come back with a much more specific question and show what you have tried. – Tim B Jan 12 '14 at 10:01
  • @Clyx: Please feel free to update you question using the example cited [here](http://stackoverflow.com/a/21073559/230513) – trashgod Jan 12 '14 at 10:28
  • possible duplicate of [How to get X and Y index of element inside GridLayout?](http://stackoverflow.com/questions/7702697/how-to-get-x-and-y-index-of-element-inside-gridlayout) – trashgod Jan 12 '14 at 10:56

1 Answers1

2

Each GridButton "knows its location on the grid." The complete example shows how to create the buttons in a loop and how to calculate grid coordinates in a List<GridButton>.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045