Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes textbox loop naming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "textbox loop naming" Watch "textbox loop naming" New topic

textbox loop naming

jason gass

Joined: Nov 11, 2004
Posts: 13
can you do a loop like this:

for (int i = 1; i<=10; i++)
textPanel.add (new JTextField(i + "") );

and name them in order, like text1,text2, text3?

So you can then refer to them like:

text1.addActionListener( textHandler );
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
If you create the references inside the loop you will not have them outside the loop.

You could create the references earlier and instantiate them (use the "new" keyword) inside a loop.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
You can access the JTextFields, without naming them

It is sorta covered in the JavaRanch Style Guide.
subject: textbox loop naming
It's not a secret anymore!