Results 1 to 4 of 4
  1. #1
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default how to create many button with different name and variable with for loop?

    This one i am really no idea how to make it...
    Now i am using this:
    Java Code:
    for (int i = 0; i < line.length; i++) 
    {
    Panel1.add(new JButton(sr));
    }
    but now i dont know how to add actionlistener to all the JButton...
    I hope anyone can help me with it.
    Thanks.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: how to create many button with different name and variable with for loop?

    Create an array of JButtons, then initialize them and add them to the panel in the for loop. You can also attach the actionlisteners in the loop -- or outside of it as well.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: how to create many button with different name and variable with for loop?

    See my example is this posting: Basic Calculator

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. multiply variable in for loop
    By aconti in forum New To Java
    Replies: 7
    Last Post: 06-03-2011, 12:24 PM
  3. Accessing a variable from a loop
    By mhz041986 in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 08:49 AM
  4. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 AM
  5. Create a new variable
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:48 AM

Posting Permissions

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