Meaningless Drivel is fun!*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Similar Threads
Create a grid and capture values under them
How to make a text editor using GEF
GlassPane displays flickering
creating textbox
Rules declaration in text file - please help