I have a 6x5 grid of JTextFields. I filled the grid quickly in the
constructor by using a FOR loop in the constructor. The code is:
JTextField [] text=new JTextField[30];
for (int i=0;i<30;i++){
text[i] =new JTextField(5);
textEntry[i]=text[i].getText();
panel_3.add(text[i],5); }
I want to use the text fields for user input and get the user input when a
button is pushed. For example, in the button event I tried String
str=text[1].getText();.
The code will not compile because the text[1] variable scope is limited to
the FOR loop in the constructor.
Initializing 30 text fields does not seem like a lot. What if there were
over a hundred? Would each text field have to be initialized one by one,
rather than in a loop? Anyone have any shortcuts?
Thanks,
Vince
> I have a 6x5 grid of JTextFields. I filled the grid quickly in the
> constructor by using a FOR loop in the constructor. The code is:
[quoted text clipped - 11 lines]
> The code will not compile because the text[1] variable scope is limited to
> the FOR loop in the constructor.
Say what? No, it is not -- you declared your "text" array outside the
for-loop, so its scope is not limited as you suggest. To sort this out,
post a short, complete, compilable example program that shows the problem.
Also, what do you mean "the code will not compile"? What error message was
printed onto your computer monitor? Perhaps you could share it with us?
> Initializing 30 text fields does not seem like a lot.
State the problem you are having. You are complaining about a problem you
have not identified.
> What if there were
> over a hundred? Would each text field have to be initialized one by one,
> rather than in a loop?
Why are you asking this? The problem does not exist.
> Anyone have any shortcuts?
Just one. Post your code.

Signature
Paul Lutus
http://www.arachnoid.com
I finally got it to work. I made variable declarations in the constructor
instead of before the constructor.
> I have a 6x5 grid of JTextFields. I filled the grid quickly in the
> constructor by using a FOR loop in the constructor. The code is:
[quoted text clipped - 18 lines]
> Thanks,
> Vince