Results 1 to 4 of 4
  1. #1
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Wink Incrementing jbutton in for loop

    Hey,
    I'm a little stuck on how to increment certain things in a loop and if it's actually possible. I understand how to increment arrays and things which take a value however when it comes to incrementing a number within the name of a declared variable etc I'm completely lost, this is what I need to do.

    I have 10 jbuttons, from button1 to button10, I want to change the icon of each button using an array.

    Java Code:
    iconArray = (1,2,1,6,8,7,1,9,7,3);
    
    for(int i=0; i<iconArray.length, i++){
        Button[B][i][/B].setIcon(iconArray[i]+".jpg");          
    }
    The above might be written wrong but I've had to simplyfy it, it's working fine other than the incrementing of the button name. I'd like the result to be:
    Java Code:
    Button[B]1[/B].setIcon(1.jpg);
    Button[B]2[/B].setIcon(2.jpg);
    Button[B]3[/B].setIcon(1.jpg);
    Button[B]4[/B].setIcon(6.jpg);
    And so on...
    I'm not even sure if this is possible in a loop? I appreciate all help!
    Thanks =]
    Last edited by RobertF; 03-12-2009 at 10:44 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As long as you have the Buttons in an array as well, yes, but if you want to try and do an eval type statement to resolve the variable "name" (God I hate that term as there is, actually, no such thing), no, you can't.

  3. #3
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    I never thought of that! How do I go about putting the buttons into an array?

    I've tried

    String[] buttonArray = { Button1, Button2, Button3, Button4, Button5... };

    but obviously they aren't strings =/


    ---edit

    stupid me, just figured it out:
    JButton[] buttonArray = { Button1, Button2, Button3, Button4, Button5... };

    =D thanks so much for your help!
    Last edited by RobertF; 03-12-2009 at 11:22 AM.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Uhm,
    Java Code:
    JButton[] buttonArray = { new JButton(), new JButton(), ... };
    maybe?

Similar Threads

  1. Replies: 1
    Last Post: 05-29-2008, 06:36 PM
  2. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM
  3. JButton onClick?
    By Joe2003 in forum AWT / Swing
    Replies: 2
    Last Post: 01-06-2008, 03:04 PM
  4. mouse over on JButton
    By gradon in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:50 AM
  5. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 10:19 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
  •