Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Add alot of buttons to GUI

Hey I want to make a program with a lot of buttons and rather than declaring and adding each one separately I want to use two for loops one for row and one for columns but I'm not sure how do I add them to the pane do I add them inside the for loops for declaring them or a separate loop for the adding??
You can't declare variables dynamically, but you CAN dynamically initialise and add them to the pane in the for loop. You could declare a multidimensional array of JBUTTON for rows and columns, then initialise them in the for loop like this:
Code
JButton[][] buttons = new JButton[6][6];
for(int i = 0; i < 6; I++) {
    for(int j = 0; j < 6; j++) {
      buttons[I][j] = new JButton();
      panel.addComponent(buttons[I][j]);
    }
}
make sure you add the panel where you added the buttons to your JFrame
Sorry for the delay replying to this thread been really busy last few days thanks for the posts exactly what I was looking for
(Mar 20, 2014 12:22 AM)Pacina Wrote:  Sorry for the delay replying to this thread been really busy last few days thanks for the posts exactly what I was looking for

Sweet, glad to help out 🙂 PM me if you have any more questions


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by going clearJan 16, 2021 12:16 PM 2 134 Jan 16, 2021 12:16 PM
Last Post: HFDone
[Python] by CryptMay 28, 2018 07:49 PM 9 260 May 28, 2018 07:49 PM
Last Post: quarkE
[.NET] by DailyTrendsJun 10, 2017 03:21 PM 4 172 Jun 10, 2017 03:21 PM
Last Post: fxbghacker