Results 1 to 8 of 8
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default "dynamic variable naming" ?

    Hi,
    I have 7 buttons (but1, but2 etc till but7) and I am trying to do a setText() for each of them in a loop like this:

    for(int i=0;i<theScale.length;i++){
    but[i+1].setText(theScale[i]);
    }
    but I think I am using the wrong syntax to add the number here: but[i+1]


    How do I do this?
    (Would also appreciate it if you could tell me what this is called)

    Thanks!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    Create an array of buttons and loop through them that way, otherwise with the naming scheme you have you will have to set each button individually. See Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

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

    Default

    Quote Originally Posted by N00Bie View Post
    Hi,
    I have 7 buttons (but1, but2 etc till but7) and I am trying to do a setText() for each of them in a loop like this:



    but I think I am using the wrong syntax to add the number here: but[i+1]


    How do I do this?
    (Would also appreciate it if you could tell me what this is called)

    Thanks!
    It's called an array; you want an array of JButtons so you can get them by the expressions but[0], but[1], ... but[6] (arrays use zero based indexes). Note that creating the array is nothing more than creating the 'slots' that can hold something but they don't hold it yet. Something like this:

    Java Code:
    JButton[] but= new JButton[7]; // create 7 slots that can hold JButtons
    
    for (int i= 0; i < but.length; i++) // assign a JButton to each slot
       but[i]= new JButton(""+i);
    I think you can take it from here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    This is my array:
    String[] theScale=new String[]{"A","B","C#","D","E","F#","G#"};
    and the buttons are already positioned on the form (and their actions/events running smooth), I just have to set the text of each of those buttons for the user to see and I need to do it this way.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    I just have to set the text of each of those buttons for the user to see and I need to do it this way.
    Then use an array, or set each button individually. Although a bit messy, you don't even have to rename the variables - just create an array that references them and use that array to loop through.

  6. #6
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    Then use an array, or set each button individually. Although a bit messy, you don't even have to rename the variables - just create an array that references them and use that array to loop through.
    Ok, that worked, thanks!

    In PHP there was a way to do what I asked, thats why I was confused... I cant remember what it was called in php either but I can search if you are interested.

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

    Default

    Quote Originally Posted by N00Bie View Post
    Ok, that worked, thanks!

    In PHP there was a way to do what I asked, thats why I was confused... I cant remember what it was called in php either but I can search if you are interested.
    Php is an interpreted language; such languages have the 'advantage' (mind the quotes) that the names of the variables are available during runtime. Compiled languages (such as Java) don't have that. The advantage of compiled langiuages over interpreted languages is runtime speed. Although you can find the names of variables (not local variables!) through reflection it is considered VBD (Very Bad Design (tm)) to do it this way.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 01-28-2011, 01:09 AM
  2. "Dynamic" Java Annotations - possible?
    By CChange in forum Advanced Java
    Replies: 6
    Last Post: 03-23-2010, 05:27 PM
  3. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 PM
  4. Replies: 2
    Last Post: 08-07-2009, 10:21 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •