Results 1 to 2 of 2

Thread: [Java] Concat Help

  1. #1
    Junior Member
    Join Date
    Apr 2004

    [Java] Concat Help

    JButton cb0 = new JButton ();
    JButton cb1 = new JButton ();
    JButton cb2 = new JButton ();
    String array1 = {"yes", "no", "maybe"};
    What I'm trying to do over here is add the strings from array1 into the buttons cb0, cb1, and cb2, such that cb0's text will be "yes", cb1's text "no" and so on. How can I do this using a for loop?

    I have a very vague idea on how to do this. I can't figure out how to go to the next button using i. My attempt at a concat is shown below. As an example, the first time it would read cb0.setText (array1 [0]);, the next time cb1.setText (array1 [1]);

    for (int i = 0; i < 3; i++)
        cb + i.setText (array1 [i]);
    Please let me know if I didn't explain it clearly.


  2. #2
    Senior Member
    Join Date
    Nov 2001
    I get what you said, and unfortunately Java lacks the eval() function that could be used for that. You really should do:
    JButton cb0 = new JButton("yes");
    JButton cb1 = new JButton("no");
    JButton cb2 = new JButton("maybe");
    unless you absolutely must be able to change it, in which case you could do:
    If you really are needing to do it in an array, you could always create an array of buttons:
    JButton[] btnarray;
    String[] btnlabels = {"yes", "no", "maybe"};
    for (int i=0; i < btnlabels.length(); i++) {
       btnarray[i] = new JButton(btnlabels[i]);
    If you have a good reference guide, this should be covered in it. If not, I recommend Java: How to Program by Deitel & Deitel.
    Chris Shepherd
    The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
    \"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
    Is your whole family retarded, or did they just catch it from you?

Posting Permissions

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