Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Easier way of creating multiple buttons

  1. #1
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Easier way of creating multiple buttons

    I need to make 50 JButtons inside a JPanel aligned like this: 5 columns, 10 rows. Does anyone know a way easier then just creating them one by one like this:

    Java Code:
    Jbutton b1=new JButton();
    b1.setBounds(0,0,50,50);
    frame.add(b1);
    
    JButton b2=new JButton();
    b2.setBounds(60,0,50,50);
    frame.add(b2);
    
    etc.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Easier way of creating multiple buttons

    Use a GridLayout where you put the buttons in and use a loop to create the buttons.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: Easier way of creating multiple buttons

    The question is, how can I make the loop? how do I name the buttons inside the loop?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Easier way of creating multiple buttons

    Quote Originally Posted by alexix View Post
    The question is, how can I make the loop? how do I name the buttons inside the loop?
    Use an array of buttons or another container (a List<JButton>?)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: Easier way of creating multiple buttons

    I tried it now but it still doesn't work, when I compile it it tells me Build Successful, no errors, but my frame won't pop up.

    Here is my code regarding the buttons:

    Java Code:
                    final JPanel panel = new JPanel();
                    frame.add(panel);
    
                    panel.setBounds(20,90,300,280);
                    panel.setLayout(new GridLayout(10,5));
                   
                    
                    JButton[] but = new JButton[50];
                   
                    panel.add(but[0]);   
                    
                    for(int i=0;i<49;i++)
                    {   //panel.add(but[i];
                    }
    If I comment the line " panel.add(but[0]); " my frame pops up normally after compile

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Easier way of creating multiple buttons

    You have created the array but you haven't created the buttons for it yet, so each array element is still null.

    kind regards,

    Jos
    alexix likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: Easier way of creating multiple buttons

    Thank you kind sir. You have been very helpful.

Similar Threads

  1. Best Way to Run a method on multiple buttons
    By Dcalladi in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 04:45 AM
  2. multiple submit buttons on webpage
    By samanthamaryhorgan in forum Advanced Java
    Replies: 1
    Last Post: 03-04-2010, 03:51 AM
  3. multiple submit buttons on webpage
    By samanthamaryhorgan in forum New To Java
    Replies: 0
    Last Post: 03-04-2010, 02:19 AM
  4. One controller with multiple buttons and colors
    By Cutter in forum New To Java
    Replies: 5
    Last Post: 01-24-2010, 07:47 PM
  5. creating buttons
    By defactor in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 03:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •