Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

    hello. This must be pretty elementary. I have a JFrame jf and JPanel jp on it. jp has five TextFields named cel1, cel2.. cel5. I wish to construct a String Cel + for loop index and run a for loop to reset the values of all the text fields using a single statement such as cel1.SetText("abc"). Similar things can be done in foxfro. How does one do it in java?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

    run a for loop to reset the values of all the text fields
    Place your JTextField's within an array and loop over the array.

  3. The Following User Says Thank You to copeg For This Useful Post:

    ydandurkar (May 23rd, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

    Thanks copeg for your suggestion and very quick reply while I was still struggling.
    I created an array of text fields TextField CellArr[] = new TextField[6];
    I initialized them CellArr[1] = cell1; ....
    and then I asked for two changes CellArr[i].setEditable(true); CellArr[i].setText("abcd");

    program goes into a long tell spin not worth reproducing.

    Anyway coming to my main question. can i create a series of strings which are actually names of my variables and get then utilize them for doing whatever else. This would greatly curtail my code writing effort.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

    can i create a series of strings which are actually names of my variables and get then utilize them
    No, variable names are created when they are typed into the source file. They can not be created when the code executes.

    Use arrays instead of having tons of variable names.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ydandurkar (May 23rd, 2014)

  7. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

    in visual fox pro this is a pass.
    x = 8
    a = "x"
    ? a ans x
    ? x ans 8
    ? &a ans 8

    becomes a very convenient programing device. anything in java which can help similar things

  8. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

    program goes into a long tell spin not worth reproducing.
    I recommend tackling what can be achieved and is standard practice rather than focusing your efforts to try and achieve something that is not standard practice and if at all possible not easy to achieve - if you are having problems using an array then I suggest to post your updated code and describe the specific problems you are having.

  9. The Following User Says Thank You to copeg For This Useful Post:

    ydandurkar (May 24th, 2014)

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: how to use value of a String variable "Cel1" as a variable name like Cel1.SetText("abc")

    Not knowing the language and not seeing any comments in the posted code, I have no idea what the code is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    ydandurkar (May 24th, 2014)

Similar Threads

  1. Replies: 2
    Last Post: May 22nd, 2014, 02:17 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 11:30 AM
  3. Replies: 8
    Last Post: May 6th, 2013, 08:53 AM
  4. Compiler refuses to "accept" my string variable - need help
    By khelben1979 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 27th, 2012, 10:33 AM
  5. When to use new String("abc")
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: July 2nd, 2012, 10:47 AM

Tags for this Thread