-
[Java] Concat Help
Code:
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]);
Code:
for (int i = 0; i < 3; i++)
{
cb + i.setText (array1 [i]);
}
Please let me know if I didn't explain it clearly.
-0
-
I get what you said, and unfortunately Java lacks the eval() function that could be used for that. You really should do:
Code:
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:
Code:
cb0.setText("yes");
If you really are needing to do it in an array, you could always create an array of buttons:
Code:
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.